Get shortcut animations Same as Phibrow app

I want to create the same animation as in the Phibrow App . See the following video for more information: - https://www.dropbox.com/s/wwc69a9ktaa52je/IMG_0155.MOV?dl=0

enter image description here enter image description here enter image description here

I tried to get this animation using UIPinchGestureRecognizer , UIRotationGestureRecognizer and UIPanGestureRecognizer . But failed to get this animation.

 func handlePinchGesture(gesture: UIPinchGestureRecognizer) { if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{ //print("UIPinchGestureRecognizer") gesture.view?.transform = (gesture.view?.transform)!.scaledBy(x: gesture.scale, y: gesture.scale) gesture.scale = 1.0 } } func handleRotationGesture(gesture: UIRotationGestureRecognizer) { if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{ print("UIRotationGestureRecognizer : ", gesture.rotation) gesture.view?.transform = (gesture.view?.transform)!.rotated(by: gesture.rotation) gesture.rotation = 0 } } func handlePanGesture(gesture: UIPanGestureRecognizer) { if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{ //print("UIPanGestureRecognizer") let translation = gesture.translation(in: view) gesture.view?.transform = (gesture.view?.transform)!.translatedBy(x: translation.x, y: translation.y) gesture.setTranslation(CGPoint(x: 0, y: 0), in: view) } } 

Is there any way to get this animation?

+8
ios animation swift gestures
source share
3 answers

I think this code works for you. I declared two kinds of firstView and secondView and added a separate gesture for both views, for example, if in the case of panGesture, for example:

 firstPan = UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture(gesture:))) secondPan = UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture(gesture:))) firstView.addGestureRecognizer(secondPan!) secondView.addGestureRecognizer(firstPan!) 

for pinch and rotation, I used the same method as above to add gestures. and for your case you need to have a slit and rotation gesture, except that the simultaneous view gesture works simultaneously for both views, so just check which view and apply the transform to another view.

 @objc func handlePinchGesture(_ gestureRecognizer: UIPinchGestureRecognizer) { guard gestureRecognizer.view != nil else { return } if gestureRecognizer.state == .began || gestureRecognizer.state == .changed { if gestureRecognizer.view == firstView { gestureRecognizer.view?.transform = (gestureRecognizer.view?.transform.scaledBy(x: gestureRecognizer.scale, y: gestureRecognizer.scale))! secondView.transform = (secondView.transform.scaledBy(x: gestureRecognizer.scale, y: gestureRecognizer.scale)) gestureRecognizer.scale = 1.0 }else{ gestureRecognizer.view?.transform = (gestureRecognizer.view?.transform.scaledBy(x: gestureRecognizer.scale, y: gestureRecognizer.scale))! firstView.transform = (firstView.transform.scaledBy(x: gestureRecognizer.scale, y: gestureRecognizer.scale)) gestureRecognizer.scale = 1.0 } } } @objc func handleRotationGesture(gesture: UIRotationGestureRecognizer) { if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{ if gesture.view == firstView { gesture.view?.transform = (gesture.view?.transform)!.rotated(by: gesture.rotation) secondView.transform = (secondView.transform).rotated(by: gesture.rotation) gesture.rotation = 0 }else{ gesture.view?.transform = (gesture.view?.transform)!.rotated(by: gesture.rotation) firstView.transform = (firstView.transform).rotated(by: gesture.rotation) gesture.rotation = 0 } } } @objc func handlePanGesture(gesture: UIPanGestureRecognizer) { if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{ let translation = gesture.translation(in: view) gesture.view?.transform = (gesture.view?.transform)!.translatedBy(x: translation.x, y: translation.y) gesture.setTranslation(CGPoint(x: 0, y: 0), in: view) } } 
+2
source share

The problem that I see is that the translation is received as gesture.translation(in: view) , for example. in the coordinate system of some view . Is view in your example a gesture.view.superview ? You are interested in how much gesture.view moves in its superview coordinate superview , so this condition must be true: view == gesture.view.superview .

The second problem is translating transform - this will be wrong if the rotation and / or scale are not identical. To correctly move gesture.view , you must change its center position, which remains correct regardless of rotation or view scale:

 func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) panRecognizer.setTranslation(panRecognizer.view.center, in: panRecognizer.view.superview) } func handlePanGesture(gesture: UIPanGestureRecognizer) { let translation = gesture.translation(in: gesture.view.superview) gesture.view.center = translation } 
+1
source share

Perhaps the problem is in order. You apply the translation after applying the rotation. On a transformation that rotates. This means that your destination will be rotated and then translated according to diet. Therefore, if you rotate the object on Float.Pi and try to move the object, it will move in the opposite direction. Instead, you need to first move the object, and then apply the existing transformation.

 @IBAction func panRecognised(_ gesture: UIPanGestureRecognizer) { if gesture.state == .began || gesture.state == .changed{ guard let targetView = gesture.view else { return } let transform = targetView.transform let translation = gesture.translation(in: view) targetView.transform = transform.concatenating(CGAffineTransform(translationX: translation.x, y: translation.y)) gesture.setTranslation(CGPoint(x: 0, y: 0), in: view) } } 
+1
source share

All Articles