Swift: How to make smooth inertia after panning(dragging)?
<p>I have been learning Swift 2.2 for several days. After finishing the official tutorial, I'm trying to make some functional samples. E.g. I am trying to make an avatar decorator.</p>
<pre><code>@IBAction func moveAvatar(recognizer:UIPanGestureRecognizer) {
    recognizer.maximumNumberOfTouches = 1
    recognizer.minimumNumberOfTouches = 1
    let debugOutOfBounds = false
    let debugGestureRecognizer = false
    let debugVelocity = true
    if (debugGestureRecognizer) {
        print(&quot;pan&quot;)
    }
    let translation = recognizer.translationInView(self.avatar)

    let standardAvatarSize:CGFloat = Conf.Size.avatarRadius * 2
    if (self.avatar.frame.width &lt;  standardAvatarSize) {
        self.avatar.frame = CGRect(origin: self.avatar.frame.origin, size: CGSize(width: standardAvatarSize, height:standardAvatarSize))
    }
    if let view = recognizer.view {
        switch recognizer.state {
        case .Began:
            if (debugVelocity) {
                print(&quot;Began: velocity: \(recognizer.velocityInView(view))  translation: \(translation)&quot;)
            }
            
            self.panAvatarLastTranslation = translation
            self.avatar.center = CGPoint(x: self.avatar.center.x + translation.x, y: self.avatar.center.y + translation.y)
        case .Changed:
            if (debugVelocity) {
                print(&quot;Changed: velocity: \(recognizer.velocityInView(view))  translation: \(translation)&quot;)
            }
            if (translation != CGPoint(x: 0.0, y: 0.0)) {
                self.panAvatarLastTranslation = translation
            }
            self.avatar.center = CGPoint(x: self.avatar.center.x + translation.x, y: self.avatar.center.y + translation.y)
        case .Ended:
            if (debugVelocity) {
                print(&quot;Ended: velocity: \(recognizer.velocityInView(view))  translation: \(translation)&quot;)
            }
            let velocityDistance = sqrt(pow(recognizer.velocityInView(view).x, CGFloat(2)) + pow(recognizer.velocityInView(view).y, CGFloat(2)))
            print(velocityDistance)
            if (velocityDistance &gt; 100) {
                let inertiaDelay: NSTimeInterval = 0.5
                let inertiaDistance : CGFloat = velocityDistance * CGFloat(inertiaDelay) * 0.02
                let inertiaX:CGFloat = self.panAvatarLastTranslation.x * inertiaDistance
                let inertiaY:CGFloat = self.panAvatarLastTranslation.y * inertiaDistance
                                    //sqrt(pow(inertiaX, CGFloat(2)) + pow(inertiaY, CGFloat(2)))
                //print(&quot;inertiaDelay: \(inertiaDelay)&quot;)
                UIView.animateWithDuration(inertiaDelay, animations: {
                    self.avatar.center.x += inertiaX
                    self.avatar.center.y += inertiaY
                })
            }
        default:
            print(&quot;default&quot;)
        }

        let standardAvatarBound: [String:CGFloat] = [
            &quot;top&quot;: UIApplication.sharedApplication().statusBarFrame.height + UIScreen.mainScreen().bounds.width / 2 - Conf.Size.avatarRadius,
            &quot;right&quot;: UIScreen.mainScreen().bounds.width / 2 + Conf.Size.avatarRadius,
            &quot;bottom&quot;: UIApplication.sharedApplication().statusBarFrame.height + UIScreen.mainScreen().bounds.width / 2 + Conf.Size.avatarRadius,
            &quot;left&quot;: UIScreen.mainScreen().bounds.width / 2 - Conf.Size.avatarRadius
        ]
        let avatarBound: [String:CGFloat] = [
            &quot;top&quot;: self.avatar.center.y - self.avatar.frame.height / 2,
            &quot;right&quot;: self.avatar.frame.width / 2 + self.avatar.center.x,
            &quot;bottom&quot;: self.avatar.center.y + self.avatar.frame.height / 2,
            &quot;left&quot;: self.avatar.center.x - self.avatar.frame.width / 2
        ]
        
        if (self.panAvatarLastTranslation.x &lt; 0 &amp;&amp; avatarBound[&quot;right&quot;] &lt; standardAvatarBound[&quot;right&quot;]) {
            UIView.animateWithDuration(0.5, animations: {
                self.avatar.center.x += (standardAvatarBound[&quot;right&quot;]! - avatarBound[&quot;right&quot;]!)
            })
            if (debugOutOfBounds) {
                print(&quot;Out of right bound&quot;)
            }
        }
        
        if(self.panAvatarLastTranslation.x &gt; 0 &amp;&amp; avatarBound[&quot;left&quot;] &gt; standardAvatarBound[&quot;left&quot;]) {
            UIView.animateWithDuration(0.5, animations: {
                self.avatar.center.x -= (avatarBound[&quot;left&quot;]! - standardAvatarBound[&quot;left&quot;]!)
            })
            if (debugOutOfBounds) {
                print(&quot;Out of left bound&quot;)
            }
        }
        
        if(self.panAvatarLastTranslation.y &gt; 0 &amp;&amp; avatarBound[&quot;top&quot;] &gt; standardAvatarBound[&quot;top&quot;]) {
            UIView.animateWithDuration(0.5, animations: {
                self.avatar.center.y -= (avatarBound[&quot;top&quot;]! - standardAvatarBound[&quot;top&quot;]!)
            })
            if (debugOutOfBounds) {
                print(&quot;Out of top bound&quot;)
            }
        }
        if(self.panAvatarLastTranslation.y &lt; 0 &amp;&amp; avatarBound[&quot;bottom&quot;] &lt; standardAvatarBound[&quot;bottom&quot;]) {
            UIView.animateWithDuration(0.5, animations: {
                self.avatar.center.y += (standardAvatarBound[&quot;bottom&quot;]! - avatarBound[&quot;bottom&quot;]!)
            })
            if (debugOutOfBounds) {
                print(&quot;Out of bottom bound&quot;)
            }
        }
    }
    recognizer.setTranslation(CGPointZero, inView: view)
}
</code></pre>
<p>Okay, as you can see that I'm handling dragging inertia in the &quot;.Ended&quot; state of the pan gesture recognizer.
It works well, but not smooth. What can I do to make a smooth panning inertia?</p>
<p>Here is the source code: <a href="https://github.com/AarioAi/NotesOpen/tree/master/Swift" rel="noreferrer">https://github.com/AarioAi/NotesOpen/tree/master/Swift</a></p>