user5513630 user5513630 - 7 months ago 23
Swift Question

Rotate the image by 180 degrees

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)
// })
}
}

}

Answer

To rotate an image you could use this snippet:

UIView.animateWithDuration(2.0, animations: {
     self.imageView.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)
})

You can adjust the animation seconds (currently 2.0).

To set the image back again use the following code:

UIView.animateWithDuration(2.0, animations: {
     self.imageV.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) * 180.0)
})