简体   繁体   English

没有setTranslation的平移手势奇怪的行为

[英]A pan gesture without setTranslation strange behaviour

My code: 我的代码:

@IBAction func handlePan(gesture: UIPanGestureRecognizer) {
    let transition = gesture.translationInView(self.view)
    switch gesture.state{
    case .Changed:
        if let view = gesture.view {
            view.center = CGPoint(x: view.center.x + transition.x, y: view.center.y + transition.y)
        }
        gesture.setTranslation(CGPointZero, inView: self.view)

    default:break
    }
}

So I can drag a big button in the screen around. 因此,我可以在屏幕上拖动一个大按钮。 Everything works find till I comment out gesture.setTranslation(CGPointZero, inView: self.view) . 一切正常,直到我注释掉gesture.setTranslation(CGPointZero, inView: self.view)

I thought that one line of code only tells the app to remember the last position of the button on screen and move from there next time, but... 我以为一行代码只会告诉应用程序记住按钮在屏幕上的最后位置,下次再从那里移开,但是...

Then I ran the project again on a simulator, when I clicked on that button and tried to move a bit, the button just flew in the same direction and disappeared off screen, why? 然后,我再次在模拟器上运行该项目,当我单击该按钮并尝试移动一点时,该按钮正向同一方向飞行并从屏幕上消失了,为什么?

As you pan around, the transition value is from where you first began the gesture. 当您平移时, transition值是从您首次开始笔势开始的地方。

Look at how you move the button. 看一下如何移动按钮。 You just keep adding the larger and larger transition to each updated center . 您只需将越来越大的transition添加到每个更新的center What you need to do is add the latest transition to the center as it was when the pan gesture was first recognized. 您需要做的是将最新的transition添加到center就像第一次识别到摇动手势时一样。

So either reset the translation (like in your posted code), or save the original center of the button when the gesture's state is .Began and apply the translation to the original center value. 因此,要么重新翻译(就像在你发布的代码),或保存原始center按钮时手势的状态是.Began和翻译向原中心值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM