首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >旋转NSView 360度

旋转NSView 360度
EN

Stack Overflow用户
提问于 2015-05-16 21:26:46
回答 1查看 660关注 0票数 1

下面的代码是用来旋转UIView 360度的。它是UIView的扩展文件。

代码语言:javascript
复制
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重新创建它,但是它似乎不使用上面的代码。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-16 22:10:24

这是可行的,但你必须改变两件事:

代码语言:javascript
复制
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 ...

代码语言:javascript
复制
    if let theLayer = self.layer {
        theLayer.addAnimation(rotateAnimation, forKey: nil)
    } 
  • 将视图设置为wantsLayertrue,以强制视图为层支持(视图在OS上不自动为层支持)。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30280947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档