How can I tell a UIGestureRecognizer to cancel an existing touch?

前端 未结 7 1045
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 03:36

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

7条回答
  •  囚心锁ツ
    2020-12-13 03:53

    According to the documentation you can subclass you gesture recogniser:

    In YourPanGestureRecognizer.m:

    #import "YourPanGestureRecognizer.h"
    
    @implementation YourPanGestureRecognizer
    
    - (void) cancelGesture {
        self.state=UIGestureRecognizerStateCancelled;
    }
    
    @end
    

    In YourPanGestureRecognizer.h:

    #import 
    #import 
    
    @interface NPPanGestureRecognizer: UIPanGestureRecognizer
    
    - (void) cancelGesture;
    
    @end
    

    Now you can call if from anywhere

    YourPanGestureRecognizer *panRecognizer = [[YourPanGestureRecognizer alloc] initWithTarget:self action:@selector(panMoved:)];
    [self.view addGestureRecognizer:panRecognizer];
    [...]
    -(void) panMoved:(YourPanGestureRecognizer*)sender {
        [sender cancelGesture]; // This will be called twice
    }
    

    Ref: https://developer.apple.com/documentation/uikit/uigesturerecognizer?language=objc

提交回复
热议问题