检测手指在UISwipeGesture[Recognizer]之后抬起的动作

5

我已经设置了一个UISwipeGestureRecognizer

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

滑动手势会使玩家向滑动方向移动。但是我需要让玩家一直保持移动状态,直到触发滑动手势的手指离开屏幕。我尝试使用touchesEnded:方法,但它要求首先进行非滑动操作。如何获取触发滑动手势的触摸事件?如何检测当手指离开屏幕时的状态?

2个回答

6

我知道你已经对这个问题有了满意的答案,但我想推荐使用UIPanGestureRecognizer而不是滑动手势。

使用平移手势识别器,直到用户停止拖动手指为止,消息会被重复发送到选择器,此时选择器将被调用一次,传递一个gesture.stateUIGestureRecognizerStateEnded的参数。例如:

- (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.
    }
}

3
在Swift语言中,这个代码变成了 if (gesture.state == UIGestureRecognizerState.Ended) 或者简写为 if (gesture.state == .Ended) - Troy

1

在查阅了苹果的文档后,我找到了UIGestureRecognizer的这个属性:

@property(nonatomic) BOOL cancelsTouchesInView

将其设置为NO,让接收器的视图处理手势识别器接收到的多点触摸序列中的所有触摸。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接