Reputation: 1699
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)
// })
}
}
}
Upvotes: 25
Views: 26191
Reputation: 26
if you want to rotate image to fit language direction when changing language you can make your image assets direction (Left to Right,Mirrors) it works perfect with me from English to arabic You can see it how to do it here
Upvotes: 0
Reputation: 1358
Swift 3
UIView.animate(withDuration:0.1, animations: {
self.arrowIcon.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
})
Upvotes: 6
Reputation: 38843
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
}
Upvotes: 68
Reputation: 27448
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 :)
Upvotes: 5