Rotate 180 degrees for UIImageView and rotate back along the same route

I got a button and want to rotate the image inside it:

self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI);

The image is an up arrow to a down arrow after a user clicks it. When I press the button again, I want the down arrow to turn up-arrow again, but it is the same as counterclockwise, but I just want the back arrow to go back up arrow

rotate back code:

self.DashButton.imageView.transform = CGAffineTransformIdentity;

I tried

self.DashButton.imageView.transform = CGAffineTransformMakeRotation(-M_PI);

What I want is not to rotate a full circle, but only 180 rotate and -180 rotate, and not rotate 360 ​​degrees completely

+6
source share
7 answers

instead of -M_PI, use -3.14159. Kind of trick, but below code saved my day.

 self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI); self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI - 3.14159); 
+16
source

you need a negative angle to rotate it in the opposite direction.

CGAffineTransformMakeRotation (180 * CGFloat (M_PI / 180))

CGAffineTransformMakeRotation (-1 * CGFloat (M_PI / 180))

take a look at this answer for an explanation About the direction of rotation of the CGAffineTransformMakeRotation?

+3
source
 self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI); self.DashButton.imageView.transform = CGAffineTransformMakeRotation(0); 
+3
source

how about this?

 self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI); self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI * 2); 

he is working on my code :)

+1
source

try it

 arrowLeft.transform = CGAffineTransformMakeRotation(180 *M_PI / 180.0); arrowLeft.transform = CGAffineTransformMakeRotation(0*M_PI/180); 
0
source

You can also use

 imageView.image = UIImage(cgImage: imageView.image!.cgImage! , scale: 1.0, orientation: .down) 
0
source

Swift 3:

Using CABasicAnimation

 var rotationAnimation = CABasicAnimation() rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z") rotationAnimation.toValue = NSNumber(value: (Double.pi)) rotationAnimation.duration = 1.0 rotationAnimation.isCumulative = true rotationAnimation.repeatCount = 100.0 view.layer.add(rotationAnimation, forKey: "rotationAnimation") 


The following are the extension functions for UIView that handle rotation start and stop operations:

 extension UIView { func startRotation() { let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z") rotation.fromValue = 0 rotation.toValue = NSNumber(value: Double.pi) rotation.duration = 1.0 rotation.isCumulative = true rotation.repeatCount = FLT_MAX self.layer.add(rotation, forKey: "rotationAnimation") } func stopRotation() { self.layer.removeAnimation(forKey: "rotationAnimation") } } 


Now using the UIView.animation closure:

 UIView.animate(withDuration: 0.5, animations: { button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi))) }) { (isAnimationComplete) in // Animation completed } 
0
source

Source: https://habr.com/ru/post/982306/


All Articles