1

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

但是它不起作用,并animationDidStop:finished:立即用finishedflag as调用NO。为什么会这样?关于我如何获得一些信息的任何指示为什么finished标志是NO

这是我使用的代码:

注意:quartzPath和来自和NSColorToCGColor的类别。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:...方法被调用。但是屏幕上仍然没有显示任何内容。finishedYES[view setWantsLayer:YES]

以下是获取CGPathReffrom的代码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

4

1 回答 1

4

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

你可以告诉你的观点,它应该由一个层支持,使用:[myView setWantsLayer:YES];

于 2013-07-03T12:37:12.177 回答