CABasicAnimation不起作用

时间:2013-07-02 14:07:18

标签: objective-c macos cocoa core-animation calayer

我正在试图制作一个圆形笔画(来自另一个SO answer)。我试图在基于Cocoa的应用程序中执行此操作。

然而,它无效,animationDidStop:finished:会立即被finished标记为NO。为什么会这样?有关如何获取finished标志为NO标志的原因的任何指示?{/ p>

以下是我使用的代码:

注意:quartzPathNSColorToCGColor来自NSColorNSBezierPath上的类别。

- (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方法。

1 个答案:

答案 0 :(得分:4)

默认情况下,OS X上的视图没有附加层,因此可能的问题是vcForCellView.view.layernil。这意味着形状图层永远不会添加到图层层次结构中,因此当动画添加到形状图层时会立即取消(finished NO所见)。

您可以告诉您的观点,它应该由使用以下内容的图层支持:[myView setWantsLayer:YES];