2014-04-10 69 views
0

我有一个UIImageView图像圈。我想长时间放大图片并将其移动到另一个地方,并在发布时放弃并保留在这个地方。但是当我松开手指时,图片移动到了起始位置。为什么?下面的代码:UIImageView上的长按手势

UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(move:)]; 
recognizer.minimumPressDuration = .5; 
recognizer.delegate = self; 
[circle addGestureRecognizer:recognizer]; 

- (void)move:(UILongPressGestureRecognizer *)sender 
{ 
    UIView *image_ = sender.view; 
    CGPoint point = [sender locationInView:image_.superview]; 
    if (sender.state == UIGestureRecognizerStateBegan) { 
     [UIView animateWithDuration:0.3 animations:^{ 
      image_.transform = CGAffineTransformMakeScale(1.2f, 1.2f); 
     } completion:nil]; 
    } 
    if (sender.state == UIGestureRecognizerStateChanged) { 
     image_.center = point; 
    } 
    if (sender.state == UIGestureRecognizerStateEnded) { 
     image_.center = point; 
     [UIView animateWithDuration:0.3 animations:^{ 
      image_.transform = CGAffineTransformMakeScale(1.f, 1.f); 
     } completion:nil]; 
    } 
} 
+0

你需要使用UIPanGesture –

回答

2

的原因,它是移动回到开始是因为你使用的是UILongPressGestureRecognizer。这种类型的识别器不会为您提供跟踪用户输入位置的可靠更新。一旦手势结束,它可能会给你手势第一次启动的地方。

我也将图像的变换设置回身份变换,而不是在两个维度上将其缩放回1.0f。

解决方法是使用UIPanGestureRecognizer并正确地跟踪手势在视图中的位置。这将工作得更好,并且最重要的是这是正确的做法。

+0

请写一个代码来实现它的例子: – SviRiDoFF

+0

使用uiPanGestureRecognizer我不能长按,但在将来我需要工作滚动手势和长按 – SviRiDoFF

0

你最好的方法是将图像视图添加到UIView。 所以它的UIView将被容器UIView和UILongPressGesture添加到它的容器视图。 然后它会工作。 UIImageview不回复UILongPressGesture,其容器视图将回复它。 你会得到你想要的结果。 希望你的成功。