Pinch, Pan and Rotate Text Simultaneously like Snapchat [SWIFT 3]
I am trying to make a TextView so you can navigate like snapchat. I did something similar to it, although when you try to scale as it rotates it tends to stretch horizontally indefinitely and dragging can sometimes be a little difficult.
I have it:
func panGesture(pan: UIPanGestureRecognizer) {
print("Being Dragged")
if pan.state == .began {
textViewOrigin = pan.location(in: textView)
}else {
let location = pan.location(in: view) // get pan location
textView.frame.origin = CGPoint(x: location.x - textViewOrigin.x, y: location.y - textViewOrigin.y)
}
}
func scaleGesture(_ gesture: UIPinchGestureRecognizer){
print("Being Scaled")
switch gesture.state{
case.began:
identity = textView.transform
case.changed,.ended:
textView.transform = identity.scaledBy(x: gesture.scale, y: gesture.scale)
default:
break
}
}
func rotationGesture(sender: UIRotationGestureRecognizer){
print("Being Rotated")
textView.transform = textView.transform.rotated(by: sender.rotation)
sender.rotation = 0
}
If anyone can help change or rewrite my code which would be great, thanks in advance!
+3
source to share
1 answer
By default, when one gesture recognizer in a view "asserts" the gesture, no one else can recognize the gesture from that point.
So, for simultaneous gesture recognizers, you need to override the function in UIGestureRecognizer delegate.
class ViewController: UIViewController,UIGestureRecognizerDelegate {
@IBOutlet var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//add pan gesture
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
gestureRecognizer.delegate = self
textField.addGestureRecognizer(gestureRecognizer)
//Enable multiple touch and user interaction for textfield
textField.isUserInteractionEnabled = true
textField.isMultipleTouchEnabled = true
//add pinch gesture
let pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(pinchRecognized(pinch:)))
pinchGesture.delegate = self
textField.addGestureRecognizer(pinchGesture)
//add rotate gesture.
let rotate = UIRotationGestureRecognizer.init(target: self, action: #selector(handleRotate(recognizer:)))
rotate.delegate = self
textField.addGestureRecognizer(rotate)
}
func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view)
// note: 'view' is optional and need to be unwrapped
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
}
}
func pinchRecognized(pinch: UIPinchGestureRecognizer) {
if let view = pinch.view {
view.transform = view.transform.scaledBy(x: pinch.scale, y: pinch.scale)
pinch.scale = 1
}
}
func handleRotate(recognizer : UIRotationGestureRecognizer) {
if let view = recognizer.view {
view.transform = view.transform.rotated(by: recognizer.rotation)
recognizer.rotation = 0
}
}
//MARK:- UIGestureRecognizerDelegate Methods
func gestureRecognizer(_: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith shouldRecognizeSimultaneouslyWithGestureRecognizer:UIGestureRecognizer) -> Bool {
return true
}
}
+9
source to share