NSImageView的连续旋转(因此它似乎是动画的) [英] Continuous Rotation of NSImageView (so it appears to be animated)
问题描述
SWIFT-OSX
我在Main.storyboard中设置了一堆imageViews.我正在尝试让它们在应用启动时旋转,我希望它们无限期地运行.我遇到过roateByAngle(angle:CGFloat),但这并没有设置动画效果,而是跳到了新角度.
I have a bunch of imageViews set in my Main.storyboard. I am trying to make them spin when the app starts and i would like them to indefinitely. I came across the roateByAngle(angle: CGFloat), but this doesn't animate it, instead it just jumps to the new angle.
我想创建两个函数spinClockwise()和spinAntiClockwise(),所以我可以在viewDidLoad中调用它们,它们将一直旋转.
I would like to make two functions, spinClockwise() and spinAntiClockwise() so I can just call them in the viewDidLoad and they will just keep turning.
我一直在使用CATransform3DMakeRotation,但似乎无法获得我想要的结果
Ive been playing with CATransform3DMakeRotation but cannot seem to get my desired results
let width = myImg.frame.width / 2
let height = myImg.frame.height / 2
myImg.layer?.transform = CATransform3DMakeRotation(180, width, height, 1)
让我知道我是否可以更具体.谢谢
Let me know if i can be more specific. Thanks
推荐答案
您可以像这样添加UIView或UIImageView的扩展名:
You could add an extension of UIView or UIImageView like this:
extension UIView {
///The less is the timeToRotate, the more fast the animation is !
func spinClockwise(timeToRotate: Double) {
startRotate(CGFloat(M_PI_2), timeToRotate: timeToRotate)
}
///The less is the timeToRotate, the more fast the animation is !
func spinAntiClockwise(timeToRotate: Double) {
startRotate(CGFloat(-M_PI_2), timeToRotate: timeToRotate)
}
func startRotate(angle: CGFloat, timeToRotate: Double) {
UIView.animateWithDuration(timeToRotate, delay: 0.0, options:[UIViewAnimationOptions.CurveLinear, UIViewAnimationOptions.Repeat], animations: {
self.transform = CGAffineTransformMakeRotation(angle)
}, completion: nil)
print("Start rotating")
}
func stopAnimations() {
self.layer.removeAllAnimations()
print("Stop rotating")
}
}
因此,当您要旋转 myImg
时,只需调用:
So when you want to rotate your myImg
, you just have to call:
myImg.spinClockwise(3)
当您要停止它时:
myImg.stopAnimations()
注意:我添加了一个游乐场只是为了您可以对其进行测试;)
NOTE: I added a playground just so you can test it out ;)
干杯!
我很糟糕,这是NSView的示例:
My bad, Here is the example for NSView:
extension NSView {
///The less is the timeToRotate, the more fast the animation is !
func spinClockwise(timeToRotate: Double) {
startRotate(CGFloat(-1 * M_PI * 2.0), timeToRotate: timeToRotate)
}
///The less is the timeToRotate, the more fast the animation is !
func spinAntiClockwise(timeToRotate: Double) {
startRotate(CGFloat(M_PI * 2.0), timeToRotate: timeToRotate)
}
func startRotate(angle: CGFloat, timeToRotate: Double) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = angle
rotateAnimation.duration = timeToRotate
rotateAnimation.repeatCount = .infinity
self.layer?.addAnimation(rotateAnimation, forKey: nil)
Swift.print("Start rotating")
}
func stopAnimations() {
self.layer?.removeAllAnimations()
Swift.print("Stop rotating")
}
}
重要说明:现在,在测试之后,我注意到您必须将NSView的锚点设置在中间,以便它可以绕其中心旋转:
Important note: Now, after my tests, I noticed that you must set the anchor point of your NSView in the middle so that it can rotate around its center:
view.layer?.anchorPoint = CGPointMake(0.5, 0.5)
我添加了一个带有 OSX示例
这篇关于NSImageView的连续旋转(因此它似乎是动画的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!