围绕中心旋转 NSView
Rotate NSView Around center
我有自定义 NSView,应该使用 CoreAnimation(或 osx sdk 中可用的任何其他方式)围绕中心旋转。我尝试了很多解决方案,但 nsview 围绕左下角而不是中心旋转,并且似乎总是忽略锚点。我做错了什么?
override func awakeFromNib() {
super.awakeFromNib()
spinnerView = NSView(frame: self.bounds)
spinnerView.layer = CALayer()
spinnerView.wantsLayer = true
addSubview(spinnerView)
spinnerView.layer!.backgroundColor = NSColor.redColor().CGColor
spinnerView.layer!.position = CGPointMake(CGRectGetMidX(spinnerView.frame), CGRectGetMidY(spinnerView.frame) )
spinnerView.layer!.anchorPoint = CGPointMake(0.5, 0.5)
startAnimation()
}
func startAnimation() {
let rotation = CABasicAnimation(keyPath: "transform")
rotation.duration = 10.0
rotation.toValue = 20 * M_PI
rotation.repeatCount = Float.infinity
rotation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
rotation.valueFunction = CAValueFunction(name: kCAValueFunctionRotateZ)
spinnerView.layer!.addAnimation(rotation, forKey: "transform.rotation.z")
}
由于 AppDelegate 中的以下代码,它似乎无法正常工作,它将 NSWindow 转换为图层支持:
mainWindow.styleMask = mainWindow.styleMask | NSFullSizeContentViewWindowMask
在 ViewController 中添加以下代码有帮助:
self.view.wantsLayer = true
我有自定义 NSView,应该使用 CoreAnimation(或 osx sdk 中可用的任何其他方式)围绕中心旋转。我尝试了很多解决方案,但 nsview 围绕左下角而不是中心旋转,并且似乎总是忽略锚点。我做错了什么?
override func awakeFromNib() {
super.awakeFromNib()
spinnerView = NSView(frame: self.bounds)
spinnerView.layer = CALayer()
spinnerView.wantsLayer = true
addSubview(spinnerView)
spinnerView.layer!.backgroundColor = NSColor.redColor().CGColor
spinnerView.layer!.position = CGPointMake(CGRectGetMidX(spinnerView.frame), CGRectGetMidY(spinnerView.frame) )
spinnerView.layer!.anchorPoint = CGPointMake(0.5, 0.5)
startAnimation()
}
func startAnimation() {
let rotation = CABasicAnimation(keyPath: "transform")
rotation.duration = 10.0
rotation.toValue = 20 * M_PI
rotation.repeatCount = Float.infinity
rotation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
rotation.valueFunction = CAValueFunction(name: kCAValueFunctionRotateZ)
spinnerView.layer!.addAnimation(rotation, forKey: "transform.rotation.z")
}
由于 AppDelegate 中的以下代码,它似乎无法正常工作,它将 NSWindow 转换为图层支持:
mainWindow.styleMask = mainWindow.styleMask | NSFullSizeContentViewWindowMask
在 ViewController 中添加以下代码有帮助:
self.view.wantsLayer = true