如何限制特定视图中的手势识别器

时间:2012-10-20 13:03:22

标签: iphone uiimageview uiimage ios6 uigesturerecognizer

我设置了一个imageView,并在该imageView上添加了UIViewUIView是透明的,现在我在ImageViews上动态添加UIView

我想在我动态添加的ImageViews上设置Pan和Pinch Zoom手势识别器,但我不知道如何限制手势识别器的操作范围。?

当使用平移手势拖动图像时,它应保持在UIView范围内,不得拖出UIView的边框。

我的UIView只占屏幕的一半。

关于我们如何实现这一目标的任何想法?

1 个答案:

答案 0 :(得分:2)

你必须在你的平移手势方法中制定条件

 CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:your view];

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
    firstX = [your image center].x;//firstX and FirstY is CGFLoat...
    firstY = [your image center].y;

}

if([(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateEnded)
{
 }
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);

    if(translatedPoint.x < 0 || translatedPoint.x > 320)
    {
          set your image frame
    }
    else if(translatedPoint.y < 0 || translatedPoint.y > 480)
    {
        set your image frame

    }

     //   [your image setCenter:translatedPoint];

  you can change  boundaries in above conditions

让我知道它是否正常工作...... !!! 快乐的编码。!!!!