ios 快速动画旋转 UIImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31478630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
animate rotation UIImageView in swift
提问by Godfather
I'm trying to animate a rotation of 180 degrees of a UIImageView
in Swift
我正在尝试UIImageView
在 Swift 中为 a 的 180 度旋转设置动画
UIView.animateWithDuration(1.0, animations: { () -> Void in
self.arrowImageView.transform = CGAffineTransformMakeRotation(CGFloat(180 * M_PI))
}) { (succeed) -> Void in
}
But isnt animating at all.
I want to use animateWithDuration cause i want to make it back at some point using CGAffineTransformMakeIdentity
但根本就没有动画。我想使用 animateWithDuration 因为我想在某个时候使用CGAffineTransformMakeIdentity
Using UIView
animations works.
使用UIView
动画作品。
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1)
UIView.setAnimationCurve(UIViewAnimationCurve.EaseIn)
let radians = CGFloat(180 * M_PI / 180)
arrowImageView.transform = CGAffineTransformMakeRotation(radians)
UIView.commitAnimations()
回答by Usman Nisar
Here is code to rotate imageview. swift 3.0
这是旋转图像视图的代码。迅捷 3.0
UIView.animate(withDuration:2.0, animations: {
self.dropDownImage.transform = CGAffineTransform(rotationAngle: CGFloat(imageRotation))
})
回答by Juan Carlos Ospina Gonzalez
Updated swift 4.0 version:
更新 swift 4.0 版本:
let rotation: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotation.toValue = Double.pi * 2
rotation.duration = 0.25 // or however long you want ...
rotation.isCumulative = true
rotation.repeatCount = Float.greatestFiniteMagnitude
yourView.layer.add(rotation, forKey: "rotationAnimation")
回答by Rakshitha Muranga Rodrigo
======= Extension for UIView=======
======= UIView 的扩展========
I found this answer from the Internet, and I test it, working perfectly fine. Hope this will help to any one, just change the UIViewto your need such as, UIView, UIButton, UIImageView, etc. and access with the myUIView.rotate()
function, here myUIViewshould be replacewith your View name(IBOutlet -> name) with correct view type for extension. This is the linkthat I found this answer. And this method is easy and working!
我从互联网上找到了这个答案,并对其进行了测试,效果很好。希望这对任何人都有帮助,只需将UIView更改为您的需要,例如UIView、UIButton、UIImageView等并使用该myUIView.rotate()
函数访问,此处myUIView应替换为您的视图名称(IBOutlet -> 名称)并具有正确的视图输入扩展名。这是我找到这个答案的链接。而且这个方法简单有效!
extension UIView{
func rotate() {
let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotation.toValue = NSNumber(double: M_PI * 2)
rotation.duration = 1
rotation.cumulative = true
rotation.repeatCount = FLT_MAX
self.layer.addAnimation(rotation, forKey: "rotationAnimation")
}
}
And then call that extension like follow:
然后调用该扩展,如下所示:
self.your_UIViewOutletName_myUIView_here.rotate()
回答by Matt
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.
首先,如果您想旋转 180 度,则必须转换为弧度。180 度的弧度是pi
. 360 度将是2 * pi
。180 * pi
将使您的动画在一秒钟内旋转 90 次左右,并以原始方向结束。
Secondly, I'm not sure why your code isn't working, but I know that the code below does work:
其次,我不确定为什么您的代码不起作用,但我知道下面的代码确实有效:
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)
回答by Allen
You left out the / 180
on CGFloat(180 * M_PI)
. Try:
你忽略了/ 180
on CGFloat(180 * M_PI)
。尝试:
UIView.animate(withDuration: 1.0) {[weak self] in
self?.arrowImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
}
回答by uplearnedu.com
Swift 5.0
斯威夫特 5.0
extension UIImageView{
func rotate() {
let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotation.toValue = NSNumber(value: Double.pi * 2)
rotation.duration = 2
rotation.isCumulative = true
rotation.repeatCount = Float.greatestFiniteMagnitude
self.layer.add(rotation, forKey: "rotationAnimation")
}
}
Updating to @Rakshitha Muranga Rodrigo answer.
更新到@Rakshitha Muranga Rodrigo 的回答。
回答by KIDdAe
Be sure to be in the Main thread
一定要在主线程
DispatchQueue.main.async {
UIView.animateWithDuration(1.0, animations: { () -> Void in
self.arrowImageView.transform = CGAffineTransformMakeRotation(CGFloat(180 * M_PI))
}) { (succeed) -> Void in
}
}
回答by NazarYavornytskyy
For me the best and shortest solution is (Swift 3/4):
对我来说,最好和最短的解决方案是(Swift 3/4):
self.YourImageView.transform = showing ? CGAffineTransform(rotationAngle: CGFloat.pi) : CGAffineTransform.identity
"showing" it's a value to determine is image rotated before or not - you can use your own logic here
“显示”这是一个确定图像之前是否旋转的值 - 您可以在此处使用自己的逻辑
回答by IvanPavliuk
90 degrees rotation
90度旋转
var shouldRotateArrow = false {
didSet {
rotateArrow()
}
}
private func rotateArrow() {
let angle: CGFloat = shouldRotateArrow ? .pi / 2 : 0
UIView.animate(withDuration: Constants.arrowRotationDuration) {
self.arrowImageView.transform = CGAffineTransform(rotationAngle: angle)
}
}
回答by Jacob King
Try calling arrowImageView.layoutSubviews(), see below:
尝试调用 arrowImageView.layoutSubviews(),见下文:
UIView.animateWithDuration(1.0, animations: { () -> Void in
self.arrowImageView.transform = CGAffineTransformMakeRotation(CGFloat(180 * M_PI))
self.arrowImageView.layoutSubviews()
}) { (succeed) -> Void in
}