Cancel touch on UIButton on touch move (iOS)

拥有回忆 提交于 2020-01-03 05:37:08

问题


I have a view with button and touch-move gesture. When user touches button it becomes selected and keeps receiving touch-move events. I want to deselect the button on touch-move and pass moves to gesture.

How to cancel touch receiving on button "elegantly"?


回答1:


You need to implement the UIGestureRecognizerDelegate. Specifically this method. Then just build your logic for deciding if the gesture should receive the touch or not. If you return NO then the touch is up for grabs for the button. if you return YES the gesture to take the touch.

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

If you never even more control I think you'll need to subclass the button and takeover its hit testing.




回答2:


Try the next line of code on Swift 4

youButton.touchesCancelled([], with: nil)

Works well with pan gesture reco



来源:https://stackoverflow.com/questions/10774397/cancel-touch-on-uibutton-on-touch-move-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!