//イベントトラフ@selector([移動:イベント])を に送信する必要があります。NSTimerで@selector経由でイベントを渡す方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
moveTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(move:) userInfo:nil repeats:YES];
}
//私の移動機能
- (void)move:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (location.x > myImageView.center.x){
[UIView animateWithDuration:0.001 animations:^{
myImageView.center = CGPointMake(myImageView.center.x+5, myImageView.center.y);
}];
}
else if (location.x < myImageView.center.x){
[UIView animateWithDuration:0.001 animations:^{
myImageView.center = CGPointMake(myImageView.center.x-5, myImageView.center.y);
}];
}
if (location.y < myImageView.center.y){
[UIView animateWithDuration:0.001 animations:^{
myImageView.center = CGPointMake(myImageView.center.x, myImageView.center.y-5);
}];
}
else if (location.y > myImageView.center.y){
[UIView animateWithDuration:0.001 animations:^{
myImageView.center = CGPointMake(myImageView.center.x, myImageView.center.y+5);
}];
}
}
ありがとうございます!小さなバグが発生しました。 コード移動機能 を参照してください。画面にタッチすると、タッチした位置に画像が移動します。 少し時間がかかり、画像は左隅(0,0)に移動します。 理由が分かりますか?とにかくお返事ありがとう – majorl3oat