web-dev-qa-db-ja.com

プログラムでパンジェスチャーを送信する方法swift

PanGesture機能を備えたビューがありますが、パンジェスチャーをあるポイントから別のポイントにプログラムで送信する必要があります。 Swift特定の時間間隔のアニメーションを使用してこれを行う方法はありますか?プログラムでパンジェスチャを呼び出す私の試みは次のとおりです。

    var upPanPoint = CGPoint(x: contentView.center.x, y: contentView.center.y + 500)
    var upPan = panGestureRecognizer.setTranslation(upPanPoint, inView: self)

    onSwipe(upPan)

パンジェスチャーを認識するコードは次のとおりです。

 func onSwipe(panGestureRecognizer : UIPanGestureRecognizer!) {
    let view = panGestureRecognizer.view!
    print(view)

    switch (panGestureRecognizer.state) {
    case UIGestureRecognizerState.Began:
        if (panGestureRecognizer.locationInView(view).y < view.center.y) {
            self.viewState.rotationDirection = .RotationAwayFromCenter
        } else {
            self.viewState.rotationDirection = .RotationTowardsCenter
        }
    case UIGestureRecognizerState.Ended:
        self.finalizePosition()
    default:
        let translation : CGPoint = panGestureRecognizer.translationInView(view)
        view.center = self.viewState.originalCenter + translation
        self.rotateForTranslation(translation, withRotationDirection: self.viewState.rotationDirection)
        self.executeOnPanForTranslation(translation)
    }
}

前もって感謝します!

10
Greg Miller
// 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
    }    
}
17
Akash Raghani
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panGesture))
    self.imageView.addGestureRecognizer(panGesture)
func panGesture(sender: UIPanGestureRecognizer){
    let point = sender.location(in: view)
    let panGesture = sender.view
    panGesture?.center = point
    print(point)
}
4
Sai kumar Reddy

Swiftバージョン4.2では、以下のコードを使用してプログラムでパンジェスチャを設定できます。

let panGesture = UIPanGestureRecognizer(target: self, action:(#selector(self.handleGesture(_:))))
self.view.addGestureRecognizer(panGesture)

@objc func handleGesture(_ sender: UIPanGestureRecognizer) {

    switch sender.state {
    case .began:
    case .changed:
    case .cancelled:
    case .ended:
    default:
        break
    }
}
1
Yash