下面的代码是用来旋转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重新创建它,但是它似乎不使用上面的代码。谢谢
发布于 2015-05-16 22:10:24
这是可行的,但你必须改变两件事:
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上不自动为层支持)。https://stackoverflow.com/questions/30280947
复制相似问题