Skip to content

Instantly share code, notes, and snippets.

@rawnly
Created November 9, 2018 21:17
Show Gist options
  • Save rawnly/1c137933b9c606b6596e9150ab96911c to your computer and use it in GitHub Desktop.
Save rawnly/1c137933b9c606b6596e9150ab96911c to your computer and use it in GitHub Desktop.
extension ViewController: UIGestureRecognizerDelegate {
// that method make it works
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
@objc func handleZoom(_ gesture: UIPinchGestureRecognizer) {
switch gesture.state {
case .began, .changed:
// Only zoom in, not out
if gesture.scale >= 1 {
// Get the scale from the gesture passed in the function
let scale = gesture.scale
// use CGAffineTransform to transform the imageView
gesture.view!.transform = CGAffineTransform(scaleX: scale, y: scale)
}
// Show the overlay
UIView.animate(withDuration: 0.2) {
self.overlay.alpha = 0.8
}
break;
default:
// If the gesture has cancelled/terminated/failed or everything else that's not performing
// Smoothly restore the transform to the "original"
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
gesture.view!.transform = .identity
}) { _ in
// Hide the overlay
UIView.animate(withDuration: 0.2) {
self.overlay.alpha = 0
}
}
}
}
@objc func handlePan(_ gesture: UIPanGestureRecognizer) {
switch gesture.state {
case .began, .changed:
// Get the touch position
let translation = gesture.translation(in: imageView)
// Edit the center of the target by adding the gesture position
gesture.view!.center = CGPoint(x: imageView.center.x + translation.x, y: imageView.center.y + translation.y)
gesture.setTranslation(.zero, in: imageView)
// Show the overlay
UIView.animate(withDuration: 0.2) {
self.overlay.alpha = 0.8
}
break;
default:
// If the gesture has cancelled/terminated/failed or everything else that's not performing
// Smoothly restore the transform to the "original"
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut, animations: {
gesture.view!.center = self.view.center
gesture.setTranslation(.zero, in: self.background)
}) { _ in
// Hide the overaly
UIView.animate(withDuration: 0.2) {
self.overlay.alpha = 0
}
}
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment