Detect when the finger lifts after a UISwipeGesture[Recognizer]

南笙酒味 提交于 2019-12-13 13:09:41

问题


I've set up a UISwipeGestureRecognizer:

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:delegate action:@selector(handleSwipeGesture:)];
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[self addGestureRecognizer:swipe];
[swipe release];

A swipe makes the player move in the direction of the swipe. I need the player to keep moving, though, until the finger that made the swipe is lifted off the screen. I've tried using the touchesEnded: method but it requires that a non-swiping touch be made first. How can I get the touch that made the swipe gesture? How can I detect when that touch is lifted off the screen?


回答1:


I know you're already satisfied with an answer to this question, but I thought I might recommend using a UIPanGestureRecognizer instead of the swipe gesture.

With a pan gesture recognizer, the message is sent to the selector repeatedly until the user stops dragging their finger, at which point the selector is called one more time, passing a gesture.state of UIGestureRecognizerStateEnded. Example:

- (void)panGesture:(UIPanGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateEnded) {
        CGPoint translation = [gesture translationInView:self.view];
        //This contains the total translation of the touch from when it 
        //first recognized the gesture until now.
        //
        //e.g (5, -100) would mean the touch dragged to the right 5 points, 
        //and up 100 points.
    }
}



回答2:


After looking through Apple's documentation, I found this property of UIGestureRecognizer:

@property(nonatomic) BOOL cancelsTouchesInView

Setting it to NO lets the receiver's view handle all touches that are part of the multi-touch sequence the gesture recognizer receives.



来源:https://stackoverflow.com/questions/3427280/detect-when-the-finger-lifts-after-a-uiswipegesturerecognizer

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