why is there a delay when moving object using UIPanGestureRecognizer

前端 未结 3 528
时光说笑
时光说笑 2021-02-04 05:44

I\'m moving UIView object using UIPanGestureRecognizer - how much I drag my finger on screen, that much I move the view in the same direction (only in X - left or right, Y is no

3条回答
  •  离开以前
    2021-02-04 06:24

    I found that it was faster responding if you use just regular touchesBegan, Moved and Ended. I even subclassed a UIGestureRecognizer, and it still had lag on the panning gesture. Even though the touchesBegan within the UIGestureRecognizer would trigger on time, the state change would take a half second to change its state... It seems faster to just use a plain old TouchesBegan, especially if you're cpu is doing a lot.

    override func touchesBegan(touches: Set, withEvent event: UIEvent?)
    {
        if touches.count == 1
        {
            initialTouchLocation = (touches.first?.locationInView(self).x)!
        }
    }
    override func touchesMoved(touches: Set, 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
                }
            }
        }
    }
    

提交回复
热议问题