15

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

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

The image is a up-arrow to down-arrow after user click it When clicking the button again, I want the down-arrow rotate to up-arrow again, but it is the same counterclockwise rotation, but I just want the arrow reverse back to up-arrow

rotate back code:

self.DashButton.imageView.transform = CGAffineTransformIdentity;

I tried

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

What I want is not rotate the full circle, just 180 rotate and -180 rotate, not a 360 full rotate

Krunal
  • 73,857
  • 44
  • 240
  • 251
Wingzero
  • 9,396
  • 8
  • 36
  • 76

8 Answers8

20

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

self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI);
self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI - 3.14159);
Wingzero
  • 9,396
  • 8
  • 36
  • 76
  • This works as CoreAnimation calculates shortest transformation, and as a rounded pi is used the shortest transform is to reverse the rotation. Yet, in practice the rotation seems indistinguishable from using pi. – Johan Jan 24 '17 at 16:05
7
self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI);
self.DashButton.imageView.transform = CGAffineTransformMakeRotation(0);
Martin Tournoij
  • 24,971
  • 24
  • 101
  • 136
Si Fisher
  • 71
  • 1
  • 2
  • 1
    Welcome to Stack Overflow! Although this code may help to solve the problem, it doesn't explain _why_ and/or _how_ it answers the question. Providing this additional context would significantly improve its long-term value. Please [edit] your answer to add explanation, including what limitations and assumptions apply. – Toby Speight Aug 11 '16 at 16:38
6

what you need is 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 CGAffineTransformMakeRotation rotation direction?

Community
  • 1
  • 1
Maria
  • 4,379
  • 1
  • 24
  • 24
4

Swift 4 version of @Maria working answer

Rotate 180 degrees counter clockwise:

let transform = CGAffineTransform.identity

UIView.animate(withDuration: 0.3, animations: {
      // to rotate counterclockwise set both of these in this exact order
      self.button.transform = transform.rotated(by: 180 * CGFloat(Double.pi))
      self.button.transform = transform.rotated(by: -1 * CGFloat(Double.pi))
}

Rotate back 180 degrees clockwise:

UIView.animate(withDuration: 0.3, animations: { 
      self.button.transform = CGAffineTransform.identity
}
Lance Samaria
  • 14,568
  • 10
  • 83
  • 201
1

try this

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

arrowLeft.transform = CGAffineTransformMakeRotation(0*M_PI/180);
AdrianHor
  • 156
  • 6
0

One can also use

imageView.image = UIImage(cgImage: imageView.image!.cgImage! , scale: 1.0, orientation: .down)
Carien van Zyl
  • 2,773
  • 20
  • 29
0

how about this?

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

it works on my code :)

wsnjy
  • 144
  • 1
  • 6
-1

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")


Here is an extension functions for UIView that handles start & stop rotation 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, UIView.animation closure:

UIView.animate(withDuration: 0.5, animations: { 
      button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi))) 
}) { (isAnimationComplete) in
    // Animation completed 
}
GoRoS
  • 4,823
  • 2
  • 38
  • 62
Krunal
  • 73,857
  • 44
  • 240
  • 251