I am working with UIPanGestureRecognizer
right now and I am following THIS tutorial and I am trying to print the velocity of UIView
into labels which is in the view.
But it's printing 0.0 every time when I move the view with UIPanGestureRecognizer
as shown into below Image:
This is 1st Image where I didn't move UIView
And this is second Image where I move it:
As you can see the labels displays same message at both situations.
Here is my Simple code for this:
import UIKit
class PanViewController: UIViewController {
@IBOutlet weak var testView: UIView!
@IBOutlet weak var horizontalVelocityLabel: UILabel!
@IBOutlet weak var verticalVelocityLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let aSelector : Selector = "moveViewWithGestureRecognizer:"
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: aSelector)
self.testView.addGestureRecognizer(panGestureRecognizer)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func moveViewWithGestureRecognizer(panGestureRecognizer: UIPanGestureRecognizer){
var touchLocation : CGPoint = panGestureRecognizer.locationInView(self.view)
self.testView.center = touchLocation
var velocity : CGPoint = panGestureRecognizer.velocityInView(self.view)
// Here is the Problem
self.horizontalVelocityLabel.text = String(format: "Horizontal Velocity: %.2f points/sec", velocity.x)
self.verticalVelocityLabel.text = String(format: "Vertical Velocity: %.2f points/sec", velocity.y)
}
}
I have tried this,this and this but nothing helps me.
Please tell me what I am doing wrong?