繁体   English   中英

将图像旋转 180 度

[英]Rotate the image by 180 degrees

我有一张像下拉的图像。 最初它看起来像下拉图像所以当用户按下下拉列表时会显示一些选项。 所以我需要的是,当下拉是真的时。我意味着当用户按下下拉图像时,当下拉列表删除时,我需要将下拉图像显示为180度。姓名就像下拉为假i.same需要将图像显示为正常位置。

这种方式是否正确而不是使用更多图像? 我正在使用 swift 2.2

更新:

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

    }

要旋转图像,您可以使用以下代码段:

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

您可以调整动画秒数(当前为 2)。

要再次设置图像,请使用以下代码:

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

斯威夫特 4.x 版本:

旋转:

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

将图像设置为正常状态:

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

首先,如果您想旋转 180 度,则必须转换为弧度。 180 度的弧度是pi 360 度将是2 * pi 180 * pi将使您的动画在一秒钟内旋转 90 次左右,并以原始方向结束。

你可以做这样的事情,

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)

希望这会有所帮助:)

斯威夫特 3

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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