如何在Swift中编程发送手势?

13

我有一个具有panGesture功能的视图,但我需要以编程方式从一个点发送pan-gesture到另一个点。在Swift中是否有一种使用特定时间间隔的动画来实现这一点的方法?以下是我尝试以编程方式调用pan gesture的代码:

    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)
    }
}

也许尝试按正确顺序调用一些NSView触摸方法。 - k06a
也许你需要将 upPan.state 设置为 .Changed(或者一些不同于 .Possible 的值),以便你能够识别它。 - Marcus Rossel
3个回答

22
// 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
    }    
}

5
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panGesture))
    self.imageView.addGestureRecognizer(panGesture)
@objc func panGesture(sender: UIPanGestureRecognizer){
    let point = sender.location(in: view)
    let panGesture = sender.view
    panGesture?.center = point
    print(point)
}

1

使用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
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接