简体   繁体   中英

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

    }

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
}

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

Swift 3

UIView.animate(withDuration:0.1, animations: {
     self.arrowIcon.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM