Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically send a pangesture in swift

I have a view that has panGesture functionality, but I need to send a pan-gesture from one point to another programatically. Is there a way to do this in swift using an animation with a specific time interval? Here is my attempt at calling the pan gesture programmatically:

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

here is the code that recognizes the pan gesture:

 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)
    }
}
like image 638
Greg Miller Avatar asked Sep 03 '15 06:09

Greg Miller


3 Answers

// 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
    }    
}
like image 65
Akash Raghani Avatar answered Nov 19 '22 14:11

Akash Raghani


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)
}
like image 4
Sai kumar Reddy Avatar answered Nov 19 '22 13:11

Sai kumar Reddy


With Swift version 4.2 you can set pan gesture programmatically using below code:

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
    }
}
like image 1
Yash Avatar answered Nov 19 '22 12:11

Yash