如何获得UIPanGesture的起点并不断计算移动距离?

时间:2015-06-05 02:49:17

标签: ios swift uigesturerecognizer

如何获取UIPanGesture的起点位置并不断计算移动距离?如果你能在答案中提供一些Swift代码会更好。

2 个答案:

答案 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)")
    }