Objective c 通过NSBezierPath设置NSView动画

Objective c 通过NSBezierPath设置NSView动画,objective-c,macos,cocoa,animation,core-animation,Objective C,Macos,Cocoa,Animation,Core Animation,我有一些NSBezierPath和一些NSView。我想设置此NSView的动画,并将NEBezierPath用作动画路径。 从IOS上的UIBezierPath创建带有路径的CAKeyframeAnimation对我来说很好,但对OSX来说却不完全一样 代码如下: self.headView = [[NSView alloc] initWithFrame:NSMakeRect(100, 100, 50, 50)]; [self.headView setWantsLayer:YES]; [

我有一些NSBezierPath和一些NSView。我想设置此NSView的动画,并将NEBezierPath用作动画路径。 从IOS上的UIBezierPath创建带有路径的CAKeyframeAnimation对我来说很好,但对OSX来说却不完全一样

代码如下:

 self.headView = [[NSView alloc] initWithFrame:NSMakeRect(100, 100, 50, 50)];
 [self.headView setWantsLayer:YES];
 [self.headView.layer setBackgroundColor:[NSColor redColor].CGColor];

 [self.contentView addSubview:self.headView];

NSBezierPath *path = [NSBezierPath bezierPath];
[path moveToPoint:pathStart];
[path curveToPoint:endPoint controlPoint1:[pointValue pointValue] controlPoint2:[pointValue pointValue]];

[path setLineWidth:3.0];
[[NSColor whiteColor] set];
[path stroke];

CAKeyframeAnimation *posAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
posAnim.path = [path quartsPath];
posAnim.duration = 1.0;
posAnim.calculationMode = kCAAnimationLinear;
[self.headView.layer addAnimation:posAnim forKey:@"posAnim"];
将NSBezierPath转换为CGPathRef的过程如下:

但它根本没有动画,以下是视频:

我不明白有什么问题


另外,我更喜欢关键帧动画,因为我想在后面添加一些反弹效果。

确保头视图的superview也有一个有效层。试试这个:


self.contentView.wantsLayer=YES

确保头视图的superview也具有有效的图层。试试这个:


self.contentView.wantsLayer=YES

如何创建
self.headView.layer
?您确定它不是
nil
?更新了有关创建NSView的详细信息我想您是在假设
NSView
CALayer
上的薄饰板,就像
UIView
一样?不是的。
self.headView.layer
是如何创建的?您确定它不是
nil
?更新了有关创建NSView的详细信息我想您是在假设
NSView
CALayer
上的薄饰板,就像
UIView
一样?不是。