检测UISwipeGesture [识别器]后手指抬起的时间

时间:2010-08-06 19:55:56

标签: iphone objective-c uikit uigesturerecognizer

我已经设置了UISwipeGestureRecognizer

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

滑动可让玩家朝滑动方向移动。但是,我需要玩家继续移动,直到滑动的手指从屏幕上抬起。我尝试过使用touchesEnded:方法,但它要求首先进行非滑动触摸。如何获得滑动手势的触摸?如何检测触摸屏何时从屏幕上抬起?

2 个答案:

答案 0 :(得分:5)

我知道你已经对这个问题的答案感到满意,但我想我可能会建议使用UIPanGestureRecognizer而不是滑动手势。

使用平移手势识别器,消息将重复发送到选择器,直到用户停止拖动他们的手指,此时再次调用选择器,传递gesture.state UIGestureRecognizerStateEnded。例如:

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

答案 1 :(得分:1)

浏览Apple的文档后,我发现了UIGestureRecognizer的这个属性:

@property(nonatomic) BOOL cancelsTouchesInView

将其设置为NO可让接收者的视图处理属于手势识别器接收的多点触控序列的所有触摸。