Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

animate rotation UIImageView in swift

Tags:

ios

swift

I'm trying to animate a rotation of 180 degrees of a UIImageView in Swift

    UIView.animateWithDuration(1.0, animations: { () -> Void in
        self.arrowImageView.transform = CGAffineTransformMakeRotation(CGFloat(180 * M_PI))
    }) { (succeed) -> Void in

    }

But isnt animating at all. I want to use animateWithDuration cause i want to make it back at some point using CGAffineTransformMakeIdentity

Using UIView animations works.

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1)
    UIView.setAnimationCurve(UIViewAnimationCurve.EaseIn)
    let radians = CGFloat(180 * M_PI / 180)
    arrowImageView.transform = CGAffineTransformMakeRotation(radians)
    UIView.commitAnimations()
like image 732
Godfather Avatar asked Jul 17 '15 14:07

Godfather


3 Answers

======= Extension for UIView =======

I found this answer from the Internet, and I test it, working perfectly fine. Hope this will help to any one, just change the UIView to your need such as, UIView, UIButton, UIImageView, etc. and access with the myUIView.rotate() function, here myUIView should be replace with your View name (IBOutlet -> name) with correct view type for extension. This is the link that I found this answer. And this method is easy and working!

=== SWIFT 3 / 4 ===

extension UIView{
    func rotate() {
        let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.toValue = NSNumber(double: M_PI * 2)
        rotation.duration = 1
        rotation.cumulative = true
        rotation.repeatCount = FLT_MAX
        self.layer.addAnimation(rotation, forKey: "rotationAnimation")
    }
}

suggested improvements. Thanks for all suggestions.

=== SWIFT 5 ===

extension UIView{
    func rotate() {
        let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.toValue = NSNumber(value: Double.pi * 2)
        rotation.duration = 1
        rotation.isCumulative = true
        rotation.repeatCount = Float.greatestFiniteMagnitude
        self.layer.add(rotation, forKey: "rotationAnimation")
    }
}

And then call that extension like follow:

self.your_UIViewOutletName_myUIView_here.rotate()
like image 197
Rakshitha Muranga Rodrigo Avatar answered Oct 09 '22 12:10

Rakshitha Muranga Rodrigo


Here is code to rotate imageview. swift 3.0

UIView.animate(withDuration:2.0, animations: {
            self.dropDownImage.transform = CGAffineTransform(rotationAngle: CGFloat(imageRotation))
        })
like image 23
Usman Nisar Avatar answered Oct 09 '22 13:10

Usman Nisar


Updated swift 4.0 version:

    let rotation: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
    rotation.toValue = Double.pi * 2
    rotation.duration = 0.25 // or however long you want ...
    rotation.isCumulative = true
    rotation.repeatCount = Float.greatestFiniteMagnitude
    yourView.layer.add(rotation, forKey: "rotationAnimation")
like image 18
Juan Carlos Ospina Gonzalez Avatar answered Oct 09 '22 11:10

Juan Carlos Ospina Gonzalez