从UIPinchGestureRecognizer中排除子视图

时间:2015-05-14 06:22:22

标签: ios objective-c cocoa-touch

    UIView *superview = [[UIView alloc] initWithFrame:CGRectMake:(0, 0, 320, 480);
    UIView *subview = [[UIView alloc] initWithFrame:CGRectMake:(0, 0, 320, 480);
    UIPanGestureRecognizer *recognizer = [[UIPinchGestureRecognizer alloc] initWithTarget: self action: @selector(handlePinch);
    superview.userInteractionEnabled = YES;
    subview.userInteractionEnabled = YES;
    [superview addGestureRecognizer:recognizer];

我有一个超级视图重叠的子视图。子视图有4个需要可点击的按钮。 superview具有缩放缩放手势,可以缩放视图。但我想禁用子视图上的缩放。 识别器也会在子视图中触发,有没有办法从子视图中排除识别器?

3 个答案:

答案 0 :(得分:0)

我使用下面的简单方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (touch.view != subview) { // accept only touchs on superview, not accept touchs on subviews
        return NO;
    }

    return YES;
}

答案 1 :(得分:0)

     UIView *superview = [[UIView alloc] initWithFrame:CGRectMake:(0, 0, 320, 480);
superview.tag=1; //set Tag for superview

    UIView *subview = [[UIView alloc] initWithFrame:CGRectMake:(0, 0, 320, 480);
subview.tag==2; //set Tag for subview
    UIPanGestureRecognizer *recognizer = [[UIPinchGestureRecognizer alloc] initWithTarget: self action: @selector(handlePinch);
    superview.userInteractionEnabled = YES;
    subview.userInteractionEnabled = YES;
    [superview addGestureRecognizer:recognizer];


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
 shouldReceiveTouch:(UITouch *)touch
{

    if(touch.view .tag==2) //if subview is touched then disable pinch gesture.
    {
        return NO;

    }
    return YES;
}

答案 2 :(得分:0)

这可能不是最好的选择,但这也可行吗?其他StackOverflow用户请善待,我是新人!

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
 shouldReceiveTouch:(UITouch *)touch
{
    CGPoint touchPoint = [gestureRecognizer locationInView: superview];

    if(CGRectContainsPoint(subview.frame, touchPoint)
    {
         return NO;
    }
    return YES;
}

Apple Docs包含CGRectContainsPoint。

https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGGeometry/index.html#//apple_ref/c/func/CGRectContainsPoint