2015-09-03 13 views
10

Ho una vista con funzionalità panGesture, ma ho bisogno di inviare un pan-gesture da un punto a un altro in modo programmatico. C'è un modo per farlo rapidamente usando un'animazione con un intervallo di tempo specifico? Qui è il mio tentativo di chiamare il gesto pan di programmazione:Come inviare una pangestura al volo in modo programmatico

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

    onSwipe(upPan) 

qui è il codice che riconosce il gesto pan:

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

Grazie in anticipo!

+0

magari cercare di chiamare alcuni metodi di contatto 'NSView' nel giusto ordine. – k06a

+0

Forse dovresti impostare 'upPan.state' su' .Changed' (o qualche valore diverso da '.Possible', permettendoti di riconoscerlo). –

+0

possibile duplicato di [UITapGestureRecognizer Attiva automaticamente un tocco nella mia vista] (http://stackoverflow.com/questions/14094691/uitapgesturerecognizer-programmatically-trigger-a-tap-in-my-view) – s1ddok

risposta

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 == UIGestureRecognizerState.Began { 
     // add something you want to happen when the Label Panning has started 
    } 

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

    if panGesture.state == UIGestureRecognizerState.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 
    }  
} 
1
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) 
}