Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate the image by 180 degrees

Tags:

ios

swift

I have one image like drop down. Initially it will look like drop down image So when user press drop down list some option will show. So what I need is, when drop-down is true.I means when user press drop down image and when the list of option are showing down I need to show the drop down image to 180 degree.Same like when drop down is false I need to show the image as normal position.

Is this way is correct instead of using one more image? I am using swift 2.2

Updated :

 @IBAction func dropBtnPress(sender: AnyObject) {

            if dropDown.hidden {
                dropDown.show()
                UIView.animateWithDuration(0.0, animations: {
                    self.image.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)
                })
            } else {
                dropDown.hide()
    //            UIView.animateWithDuration(2.0, animations: {
    //                self.image.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / -180.0)
    //            })
            }
        }

    }
like image 680
user5513630 Avatar asked Apr 23 '16 06:04

user5513630


3 Answers

To rotate an image you could use this snippet:

UIView.animateWithDuration(2, animations: {      self.imageView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)) }) 

You can adjust the animation seconds (currently 2).

To set the image back again use the following code:

UIView.animateWithDuration(2, animations: {      self.imageV.transform = CGAffineTransform.identity }) 

Swift 4.x version:

Rotate:

UIView.animate(withDuration: 2) {     self.imageView.transform = CGAffineTransform(rotationAngle: .pi) } 

Set the image to it´s normal state:

UIView.animate(withDuration: 2) {     self.imageView.transform = CGAffineTransform.identity } 
like image 85
Rashwan L Avatar answered Oct 03 '22 05:10

Rashwan L


First of all, if you want to rotate 180 degrees, that has to translate to radians. 180 degrees in radians is pi. 360 degrees would be 2 * pi. 180 * pi would make your animation spin around 90 times in one second and end up with the original orientation.

You can do something like this,

let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation") rotationAnimation.fromValue = 0.0 rotationAnimation.toValue = M_PI rotationAnimation.duration = 1.0  self.arrowImageView.layer.addAnimation(rotationAnimation, forKey: nil) 

hope this will help :)

like image 27
Ketan Parmar Avatar answered Oct 03 '22 07:10

Ketan Parmar


Swift 3

UIView.animate(withDuration:0.1, animations: {
     self.arrowIcon.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
})
like image 45
Basir Alam Avatar answered Oct 03 '22 05:10

Basir Alam