簡體   English   中英

如何以編程方式快速發送一個pangesture

[英]How to programmatically send a pangesture in swift

我有一個具有 panGesture 功能的視圖,但我需要以編程方式將 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)
    }
}
// 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
    }    
}
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)
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM