【问题标题】:How to limit gesture recognizer within a specific view如何在特定视图内限制手势识别器
【发布时间】:2012-10-10 22:08:56
【问题描述】:

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

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

当使用平移手势拖动图像时,它应该保持在UIView 内,并且不能被拖出UIView 的边界。

我的UIView 只占屏幕的一半。

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

【问题讨论】:

    标签: iphone uiimageview uiimage ios6 uigesturerecognizer


    【解决方案1】:

    您必须在平移手势方法中设置条件

     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
    

    让我知道它是否工作......!!! 快乐编码.!!!!

    【讨论】:

    • 嘿,谢谢,但我使用 touchesMoved 方法实现了它。 :)
    猜你喜欢
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    相关资源
    最近更新 更多