Animating NSView via NSBezierPath



我有一些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 是从那里开始的: https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/CocoaDrawingGuide/Paths/Paths.html#//apple_ref/doc/uid/TP40003290-CH206-SW2

但它根本没有动画,这是视频:http://d.pr/v/glqQ

我不明白问题是什么...

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

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

self.contentView.wantsLayer = YES;

最新更新