3

画面上の任意の場所に触れると touchesBegan イベントがトリガーされます。しかし、UIImageView のように特定のオブジェクトに触れた場合、どうすれば管理できませんでしたか?

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

 UITouch *touch = [[event allTouches] anyObject];

 CGPoint location = [touch locationInView: touch.view];

 imageView.center = location;

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

 [self touchesBegan:touches withEvent:event];

}
4

1 に答える 1

13

OK、解決策を見つけました。コードは次のとおりです。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];

    if ([touch view] == imageView) {

        CGPoint location = [touch locationInView: self.view];

        imageView.center = location;

    }

}
于 2010-12-17T21:52:15.347 に答える