How to cancel UIGestureRecognizer if subview's button pressed

后端 未结 4 613
说谎
说谎 2021-02-04 03:27

I am struggling to get the behaviour I would like from the gesture recognisers, specifically cancelling certain gestures if others have fired.

I have a scrollView set to

4条回答
  •  梦毁少年i
    2021-02-04 03:51

    The solution that worked the best for me in the end was to use the hitTest to determine if there were any buttons underneath the location of the tap gesture. If there are then just ignore the rest of the gesture code.

    Seems to work well. Would like to know if there are any gotchas with what I have done.

    - (void) tapGestureHandler:(UIGestureRecognizer *) gestureRecognizer {
        const CGFloat kTapMargin = 180;
    
        // Get the position of the point tapped in the window co-ordinate system
        CGPoint tapPoint = [gestureRecognizer locationInView:nil];
    
        // If there are no buttons beneath this tap then move to the next page if near the page edge
        UIView *viewAtBottomOfHeirachy = [self.window hitTest:tapPoint withEvent:nil];
        if (![viewAtBottomOfHeirachy isKindOfClass:[UIButton class]]) {
    
            // If the tap point is to the left of the page then go back a page
            if (tapPoint.x > (self.bounds.size.width - kTapMargin)) [self scrollRectToVisible:pageViewRightFrame animated:YES];
    
            // If the tap point is to the right of the page then go forward a page
            else if (tapPoint.x < kTapMargin) [self scrollRectToVisible:pageViewLeftFrame animated:YES];
        }   
    }
    

提交回复
热议问题