Skip to content

Instantly share code, notes, and snippets.

@Lancewer
Created January 15, 2018 14:19
Show Gist options
  • Save Lancewer/5254bbd06b88893b59af6d1739a811f4 to your computer and use it in GitHub Desktop.
Save Lancewer/5254bbd06b88893b59af6d1739a811f4 to your computer and use it in GitHub Desktop.
[PanGestureBasicUsage] basic usage of pan gesture recongizer
This snippet shows the basic usage of pan gesture recongizer.
// The Pan Gesture
func createPanGestureRecognizer(targetView: UIImageView) {
var panGesture = UIPanGestureRecognizer(target: self, action:#selector(handlePanGesture(_:)))
targetView.addGestureRecognizer(panGesture)
}
@objc func handlePanGesture(panGesture: UIPanGestureRecognizer) {
// get translation
let translation = panGesture.translation(in: view)
// This line is reset translation point to zero, let the next translation base on
panGesture.setTranslation(CGPoint.zero, in: view)
print(translation)
// create a new Label and give it the parameters of the old one
let label = panGesture.view as! UIImageView
label.center = CGPoint(x: label.center.x+translation.x, y: label.center.y+translation.y)
label.isMultipleTouchEnabled = true
label.isUserInteractionEnabled = true
// the next line can use switch instead of if
if panGesture.state == .began {
// add something you want to happen when the Label Panning has started
}
if panGesture.state == .ended {
// add something you want to happen when the Label Panning has ended
}
if panGesture.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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment