如何获取UIPanGesture
的起点位置并不断计算移动距离?如果你能在答案中提供一些Swift代码会更好。
答案 0 :(得分:3)
override func viewDidLoad() {
super.viewDidLoad()
let pan = UIPanGestureRecognizer(target: self, action: "catchPaned")
self.view.addGestureRecognizer(pan)
self.view.userInteractionEnabled = true
}
func catchPaned(gesture:UIPanGestureRecognizer){
switch(gesture.state){
case .Began:
let touchStart = gesture.locationInView(self.view)
case .Changed:
let distance = gesture.translationInView(self.view)
default:
println("default")
}
}
使用locationInView
获取起点,使用translationInView
答案 1 :(得分:0)
我自己弄清楚了。
let panGesture = UIPanGestureRecognizer(target: self, action: Selector("panFunction:"))
myView.addGestureRecognizer(panGesture)
func panFunction(sender: UIPanGestureRecognizer) {
let translation = sender.translationInView(self.view)
let startPoint = CGPointZero
let distance = Double(translation.y - startPoint.y)
println("\(distance)")
}