首页 文章

使用UIPanGestureRecognizer移动对象时为什么会出现延迟

提问于
浏览
12

我正在使用UIPanGestureRecognizer移动UIView对象 - 我在屏幕上拖动手指多少,我将视图向同一方向移动(仅在X - 左或右,Y不变) . 它工作正常,但延迟(非常明显) .

以下是处理UIPanGestureRecognizer事件的方法:

-(void)movePages:(UIPanGestureRecognizer *)sender
{
    if(switchingMode == 1){
        if([sender state] == UIGestureRecognizerStateBegan){
            fingerStartPosition = [sender locationInView:self.view].x;
            viewStartPosition = [[viewControllers objectAtIndex:activeViewControllerIndex] view].center;
        }
        [[[[viewControllers objectAtIndex:activeViewControllerIndex] view] layer] setPosition:CGPointMake(viewStartPosition.x - (fingerStartPosition - [sender locationInView:self.view].x) , viewStartPosition.y)];

    }
}

我试图使用它的图层设置视图的位置,我也尝试使用具有不同持续时间的动画设置框架,但一切都表现相同 . 知道为什么会出现这种延迟吗?

3 回答

  • 1

    使用 UILongPressGestureRecognizer 并将 minimumPressDuration 设置为 0.0 . 这会立即识别并获得所有相同的更新,包括具有更新位置的 UIGestureRecognizerStateChanged .

  • 39

    我发现如果你只使用常规touchesBegan,Moved和Ended就会更快响应 . 我甚至将UIGestureRecognizer子类化,并且它仍然在平移手势上有延迟 . 即使UIGestureRecognizer中的touchesBegan会按时触发,状态改变也需要半秒才能改变其状态......使用普通的旧TouchesBegan似乎更快,特别是如果你的cpu做了很多 .

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        if touches.count == 1
        {
            initialTouchLocation = (touches.first?.locationInView(self).x)!
        }
    }
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        if touches.count == 1
        {
            let locationInView = touches.first?.locationInView(self)
            if !thresholdHit
            {
                //this is the threshold for x movement in order to trigger the panning...
                if abs(initialTouchLocation - locationInView!.x) > 1
                {
                    thresholdHit = true
                }
            }
            else
            {
                if (self.frame.width != CGFloat(screenSize))
                {
                    let panDelta = initialTouchLocation - locationInView!.x
                }
            }
        }
    }
    
  • 1

    如果它是一个平移手势,GestureRecognizer无法确定在您将手指移动一些像素之前 . 我不知道确切的公差值,但这就是你感到延迟的原因 .

    文档:

    平移手势是连续的 . 当允许的最小手指数移动到足以被视为平底锅时,它开始 .

    如果您想要即时移动,您可能需要使用 touchesMoved: 构建自己的逻辑 .

    另一种方法可以是动画到第一个被识别的点 . 但这并没有消除延迟 . 对于这种方法,您可以在github上查看我的JDDroppableView .

相关问题