UIButton and Swipe Gesture

2019-05-04 08:45发布

问题:

I have a UIView with two UIButtons on each side (left and right) and a UIImageView in the center. A tap on either button will change the UIImageView and that works. I want the ability swipe across the UIView as well and it change UIImageView. Right now, you have to swipe on the UIImageView itself since the UIButtons are catching the input.

I'd like to be able to perform a swipe anywhere on the containing UIView and it performs the transition. Is there a way to forward touch events such that a swipe can trigger an action, but any other touch is forwarded to the proper subviews?

回答1:

Set delaysTouchesBegan to YES on the gesture recognizer, which will make it only forward touch events to subviews if a gesture is not recognized.



回答2:

How do you currently have the UIButtons set up to receive the input? Make sure that the selector you are hooking up the UIButton is hooked up with "Touch Up Inside". This should ensure that a swipe that begins on one of the buttons does not actually activate the button (assuming the user swipes all the way off the button).