我正在试图制作一个圆形笔画(来自另一个SO answer)。我试图在基于Cocoa的应用程序中执行此操作。
然而,它无效,animationDidStop:finished:
会立即被finished
标记为NO
。为什么会这样?有关如何获取finished
标志为NO
标志的原因的任何指示?{/ p>
以下是我使用的代码:
注意:quartzPath
和NSColorToCGColor
来自NSColor
和NSBezierPath
上的类别。
- (IBAction)animateCircle:(id)sender {
int radius = 10;
circle = [CAShapeLayer layer];
// Make a circular shape
circle.path = [[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(10, 10, 2.0*radius, 2.0*radius)] quartzPath] ;
// Center the shape in self.view
circle.position = CGPointMake(CGRectGetMidX(self.vcForCellView.view.frame)-radius,
CGRectGetMidY(self.vcForCellView.view.frame)-radius);
// Configure the apperence of the circle
circle.fillColor = [NSColor NSColorToCGColor:[NSColor clearColor]];
circle.strokeColor = [NSColor NSColorToCGColor:[NSColor blackColor]];
circle.lineWidth = 5;
// Add to parent layer
[self.vcForCellView.view.layer addSublayer:circle];
// Configure animation
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 2.0; // "animate over 10 seconds or so.."
drawAnimation.repeatCount = 1.0; // Animate only once..
drawAnimation.removedOnCompletion = NO; // Remain stroked after the animation..
// Animate from no part of the stroke being drawn to the entire stroke being drawn
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:1.0f];
// Experiment with timing to get the appearence to look the way you want
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
drawAnimation.delegate = self;
// Add the animation to the circle
[circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
}
还有以下代码:
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
CALayer *layer = [anim valueForKey:@"parentLayer"];
NSLog(@"%@",layer);
NSLog(@"%@",flag?@"YES":@"NO");
}
给出以下输出:
2013-07-02 19:28:14.760 TicTacToe[16338:707] (null)
的 2013-07-02 19:28:14.761 TicTacToe[16338:707] NO
更新:
在我使用图层(animationDidStop:...
)备份视图后,finished
方法将YES
调用为[view setWantsLayer:YES]
。但屏幕上仍然没有显示任何内容。
以下是从CGPathRef
获取NSBezierPath
的代码:
- (CGPathRef)quartzPath
{
int i;
NSInteger numElements;
// Need to begin a path here.
CGPathRef immutablePath = NULL;
// Then draw the path elements.
numElements = [self elementCount];
if (numElements > 0)
{
CGMutablePathRef path = CGPathCreateMutable();
NSPoint points[3];
BOOL didClosePath = YES;
for (i = 0; i < numElements; i++)
{
switch ([self elementAtIndex:i associatedPoints:points])
{
case NSMoveToBezierPathElement:
CGPathMoveToPoint(path, NULL, points[0].x, points[0].y);
break;
case NSLineToBezierPathElement:
CGPathAddLineToPoint(path, NULL, points[0].x, points[0].y);
didClosePath = NO;
break;
case NSCurveToBezierPathElement:
CGPathAddCurveToPoint(path, NULL, points[0].x, points[0].y,
points[1].x, points[1].y,
points[2].x, points[2].y);
didClosePath = NO;
break;
case NSClosePathBezierPathElement:
CGPathCloseSubpath(path);
didClosePath = YES;
break;
}
}
// Be sure the path is closed or Quartz may not do valid hit detection.
if (!didClosePath)
CGPathCloseSubpath(path);
immutablePath = CGPathCreateCopy(path);
CGPathRelease(path);
}
return immutablePath;
}
我使用的是10.7 SDK,无法使用CGPath
的新SDK添加的内置NSBezierPath
方法。
答案 0 :(得分:4)
默认情况下,OS X上的视图没有附加层,因此可能的问题是vcForCellView.view.layer
是nil
。这意味着形状图层永远不会添加到图层层次结构中,因此当动画添加到形状图层时会立即取消(finished
NO
所见)。
您可以告诉您的观点,它应该由使用以下内容的图层支持:[myView setWantsLayer:YES];