且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何以编程方式快速发送一个pangesture

更新时间:2023-02-02 15:21:15

// The Pan Gesture
func createPanGestureRecognizer(targetView: UIImageView) {
    var panGesture = UIPanGestureRecognizer(target: self, action:("handlePanGesture:"))
    targetView.addGestureRecognizer(panGesture)
}

func handlePanGesture(panGesture: UIPanGestureRecognizer) {
    // get translation
    var translation = panGesture.translationInView(view)
    panGesture.setTranslation(CGPointZero, inView: view)
    println(translation)

    // create a new Label and give it the parameters of the old one
    var label = panGesture.view as UIImageView
    label.center = CGPoint(x: label.center.x+translation.x, y: label.center.y+translation.y)
    label.multipleTouchEnabled = true
    label.userInteractionEnabled = true

    if panGesture.state == UIGestureRecognizer.State.began { 
        // add something you want to happen when the Label Panning has started
    }

    if panGesture.state == UIGestureRecognizer.State.ended {
        // add something you want to happen when the Label Panning has ended
    }

    if panGesture.state == UIGestureRecognizer.State.changed {           
        // add something you want to happen when the Label Panning has been change ( during the moving/panning ) 
    } else {  
        // or something when its not moving
    }    
}