Swift NSImageView的连续旋转(因此看起来像是动画)
SWIFT-OSX 我在Main.storyboard中设置了一组ImageView。我试图让他们旋转时,应用程序启动,我希望他们无限期。我遇到了roateByAngle(angle:CGFloat),但它并没有设置动画,而是跳转到新的角度 我想创建两个函数,Spin顺时针()和SpinAnti顺时针(),这样我就可以在viewDidLoad中调用它们,它们会一直旋转 我一直在玩CATTransferM3dMakeRotation,但似乎无法达到我想要的效果Swift NSImageView的连续旋转(因此看起来像是动画),swift,macos,Swift,Macos,SWIFT-OSX 我在Main.storyboard中设置了一组ImageView。我试图让他们旋转时,应用程序启动,我希望他们无限期。我遇到了roateByAngle(angle:CGFloat),但它并没有设置动画,而是跳转到新的角度 我想创建两个函数,Spin顺时针()和SpinAnti顺时针(),这样我就可以在viewDidLoad中调用它们,它们会一直旋转 我一直在玩CATTransferM3dMakeRotation,但似乎无法达到我想要的效果 let width
let width = myImg.frame.width / 2
let height = myImg.frame.height / 2
myImg.layer?.transform = CATransform3DMakeRotation(180, width, height, 1)
如果我能更具体一些,请告诉我。
谢谢您可以像这样添加UIView或UIImageView的扩展:
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
时,只需调用:
myImg.顺时针旋转(3)
当你想阻止它时:
myImg.stopAnimations()
注意:
我添加了一个示例,以便您可以对其进行测试;)
干杯
编辑:
我的错,以下是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的锚点,以便它可以围绕它的中心旋转:
view.layer?.anchorPoint = CGPointMake(0.5, 0.5)
我用为我添加了一个新的操场,我无法更改锚点。它围绕(0,0)旋转,这是左下角。我将锚点移动到(0.5,0.5),但仍然没有运气。然后我偶然遇到了一个女孩。我修改了我的代码,如下所示,它开始围绕自己旋转。尽管我发现了一个缺点,视图的位置在某种程度上发生了变化,但它可以通过反复试验来修复,试图将其定位到正确的位置
extension NSView {
func startRotating(duration: Double = 1) {
let kAnimationKey = "rotation"
//self.wantsLayer = true
if layer?.animation(forKey: kAnimationKey) == nil {
var oldFrame = self.frame
self.layer?.anchorPoint = CGPoint(x: 1, y: 1)
self.frame = oldFrame
let animate = CABasicAnimation(keyPath: "transform.rotation")
animate.duration = duration
animate.repeatCount = Float.infinity
animate.fromValue = 0.0
animate.toValue = Double.pi * 2.0
self.layer?.add(animate, forKey: kAnimationKey)
oldFrame = self.frame
self.layer?.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.frame = oldFrame
}
}
}
啊,是的,这就是我想要的东西,除了它需要是NSView,尽管NSView没有
动画宽度
。有什么想法吗?@Steve:我更新了我的答案,让我知道它是否适合你!ohhh-soo-close,图像正在旋转,但我无法将其放入最初放置的帧中心,myImg.layer?.anchorPoint=CGPointMake(0.5,0.5)
但是它有点左下角了。你是否也设置了它的图层?位置让x=view.frame.width/2让y=view.frame.height/2 imgenrgyball.layer?.ancorpoint=CGPointMake(0.5,0.5)imgenrgyball.layer?.position=CGPointMake(x,y)
我这样做了,把它带到中间,但它又开始绕左下角旋转=/Thank.:)我刚试过这个,它仍然绕着一个角旋转。您如何定位您的NSView
?你使用自动布局吗?它是否包装在另一个大小相同的NSView
中?是的,我使用自动布局。在故事板上,我的视图(即NSImageView)放置在堆栈视图中。