【问题标题】:Detect when the finger lifts after a UISwipeGesture[Recognizer]在 UISwipeGesture[Recognizer] 之后检测手指何时抬起
【发布时间】:2010-08-06 19:55:56
【问题描述】:

我已经设置了UISwipeGestureRecognizer:

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

滑动使玩家朝着滑动的方向移动。不过,我需要玩家继续移动,直到进行滑动的手指从屏幕上抬起。我试过使用 touchesEnded: 方法,但它要求先进行非滑动触摸。如何获得做出滑动手势的触摸?我怎样才能检测到该触摸何时离开屏幕?

【问题讨论】:

    标签: iphone objective-c uikit uigesturerecognizer


    【解决方案1】:

    我知道您已经对这个问题的答案感到满意,但我想我可能会推荐使用 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.
        }
    }
    

    【讨论】:

    • 在 Swift 语言下,这会变成 if (gesture.state == UIGestureRecognizerState.Ended) 或简单的 if (gesture.state == .Ended)
    【解决方案2】:

    查看了苹果的文档后,我发现了 UIGestureRecognizer 的这个属性:

    @property(nonatomic) BOOL cancelsTouchesInView
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多