OGeek|极客世界-中国程序员成长平台

标题: How can I tell a UIGestureRecognizer to cancel an existing touch? [打印本页]

作者: 菜鸟教程小白    时间: 2022-5-24 13:56
标题: How can I tell a UIGestureRecognizer to cancel an existing touch?

I have a UIPanGestureRecognizer I am using to track an object (UIImageView) below a user's finger. I only care about motion on the X axis, and if the touch strays above or below the object's frame on the Y axis I want to end the touch.

I've got everything I need for determining if a touch is within the object's Y bounds, but I don't know how to cancel the touch event. Flipping the recognizer's cancelsTouchesInView property doesn't seem to do what I want.

Thanks!



Best Answer-推荐答案


This little trick works for me.

@implementation UIGestureRecognizer (Cancel)

- (void)cancel {
    self.enabled = NO;
    self.enabled = YES;
}

@end

From the UIGestureRecognizer @enabled documentation:

Disables a gesture recognizers so it does not receive touches. The default value is YES. If you change this property to NO while a gesture recognizer is currently recognizing a gesture, the gesture recognizer transitions to a cancelled state.






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4