Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将图像旋转180度_Ios_Swift - Fatal编程技术网

Ios 将图像旋转180度

Ios 将图像旋转180度,ios,swift,Ios,Swift,我有一张像drop down这样的图片。最初它看起来像是下拉图像,所以当用户按下下拉列表时,会显示一些选项。所以我需要的是,当下拉为真时。我的意思是当用户按下下拉图像时,当选项列表向下显示时,我需要将下拉图像显示为180度。与下拉为假时相同,我需要将图像显示为正常位置 这种方法正确吗,而不是使用一个以上的图像?我使用的是swift 2.2 更新: @IBAction func dropBtnPress(sender: AnyObject) { if dropDown.

我有一张像drop down这样的图片。最初它看起来像是下拉图像,所以当用户按下下拉列表时,会显示一些选项。所以我需要的是,当下拉为真时。我的意思是当用户按下下拉图像时,当选项列表向下显示时,我需要将下拉图像显示为180度。与下拉为假时相同,我需要将图像显示为正常位置

这种方法正确吗,而不是使用一个以上的图像?我使用的是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
})
Swift 4.x版本:

旋转:

UIView.animate(withDuration: 2) {
    self.imageView.transform = CGAffineTransform(rotationAngle: .pi)
}
UIView.animate(withDuration: 2) {
    self.imageView.transform = CGAffineTransform.identity
}
将图像设置为正常状态:

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)

希望这会有所帮助:)

Swift 3

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

它工作了。当我按下时,图像变为180度。但当下拉菜单再次隐藏时,它不会变为正常位置。为什么???@user5513630,检查更新以使后退按钮再次工作。
(180*x)/180=x
放下
(180.0*CGFloat(M_PI))/180.0
,只需使用
CGFloat(M_PI)