Handling gesture recognizers in iOS6

前端 未结 3 741
春和景丽
春和景丽 2021-02-07 13:19

Apparently iOS 6 tries to automatically handle the situation when you have a gesture recognizer and a UIButton in the same place, being activated for the same gestu

3条回答
  •  生来不讨喜
    2021-02-07 13:46

    Although i don't know why you would put UIButtons there if you don't want them to be tapped, you can prevent subviews from receiving touches by overriding the -hitTest:withEvent: method of your containing view.

    -hitTest:withEvent: by default returns the "farthest descendant of the receiver in the view hierarchy (including itself) that contains a specified point", so by default, when tapping one of the buttons, your containing view would return the button.

    Your implementation should look something like this:

    - (UIView *)hitTest:(CGPoint)aPoint withEvent:(UIEvent *)event {
        if ([self pointInside:aPoint]) {
            return self;
        } else {
            return nil;
        }
    }
    

    So if your container view this is being called on contains the point of the touch, it returns itself and the gesture recognizer attached to it will get the touch.

    Because this implementation never returns any subview, none of the UIButtons will ever get a chance to respond to the touches.

提交回复
热议问题