【问题标题】:Rotate NSView 360 degrees旋转 NSView 360 度
【发布时间】:2015-05-16 21:26:46
【问题描述】:

我在下面有这段代码可以将 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 重新创建它,但使用上面的代码似乎无法正常工作。 谢谢

【问题讨论】:

    标签: ios macos swift animation nsview


    【解决方案1】:

    它有效,但你必须改变两件事:

    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 上视图不会自动支持图层)。

    【讨论】:

      猜你喜欢
      • 2016-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 2012-04-08
      相关资源
      最近更新 更多