[英]Rotate NSView 360 degrees
我下面有这段代码可以将UIView旋转360度。 它是UIView的扩展文件。
extension NSView {
func rotate360Degrees(duration: CFTimeInterval = 0.5, completionDelegate: AnyObject? = nil) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(M_PI * 2.0)
rotateAnimation.duration = duration
if let delegate: AnyObject = completionDelegate {
rotateAnimation.delegate = delegate
}
self.layer.addAnimation(rotateAnimation, forKey: nil)
}
}
单击按钮后,我然后使用refreshButton.rotate360Degrees()
开始动画。
我想为NSView重新创建它,但是使用上面的代码似乎无法正常工作。 谢谢
它有效,但是您必须更改两件事:
extension NSView {
func rotate360Degrees(duration: CFTimeInterval = 0.5, completionDelegate: AnyObject? = nil) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(M_PI * 2.0)
rotateAnimation.duration = duration
if let delegate: AnyObject = completionDelegate {
rotateAnimation.delegate = delegate
}
// `addAnimation` will execute *only* if the layer exists
self.layer?.addAnimation(rotateAnimation, forKey: nil)
}
}
?
在self.layer
之后,以允许有条件执行(如果该层不可用)。 如果愿意,可以使用if let ...
:
if let theLayer = self.layer {
theLayer.addAnimation(rotateAnimation, forKey: nil)
}
wantsLayer
设置为true
,以强制视图进行分层支持(在OS X上视图不会自动进行分层支持)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.