而不是使用-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
方法使用UIPanGestureRecognizer。
創建並添加一個平移手勢您UIImageView
像:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDrag:)];
[yourImageView addGestureRecognizer:panGesture];
並寫出選擇方法,如:
- (void)handleDrag:(UIPanGestureRecognizer*)recognizer
{
CGPoint translation = [recognizer translationInView:recognizer.view];
CGPoint velocity = [recognizer velocityInView:recognizer.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
if (recognizer.state == UIGestureRecognizerStateBegan)
{
// start tracking movement
}
else if (recognizer.state == UIGestureRecognizerStateEnded)
{
// You can change position here, with animation
}
}
代替此使用UIPangesture –