简体   繁体   English

在触摸移动上取消对UIButton的触摸(iOS)

[英]Cancel touch on UIButton on touch move (iOS)

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"? 如何取消“优雅”按钮上的触摸接收?

You need to implement the UIGestureRecognizerDelegate. 您需要实现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. 如果您再也无法控制,我认为您需要对按钮进行子类化,并接管其点击测试。

Try the next line of code on Swift 4 在Swift 4上尝试下一行代码

youButton.touchesCancelled([], with: nil)

Works well with pan gesture reco 与平移手势记录一起很好地工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM