首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CABasicAnimation不工作

CABasicAnimation不工作
EN

Stack Overflow用户
提问于 2013-07-02 14:07:17
回答 1查看 1.6K关注 0票数 1

我试图动画一个圆形的笔画(从另一个所以answer)。我正试图在一个基于可可的应用程序中这样做。

但是,它不起作用,animationDidStop:finished:立即被finished标记调用为NO。为什么会发生这种情况?关于如何获得finished标志为NO的一些信息的任何提示

下面是我使用的代码:

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

代码语言:javascript
复制
- (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"];

}

还有以下代码:

代码语言:javascript
复制
-(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:... )支持视图( [view setWantsLayer:YES])之后,finished作为YES调用了该方法。但屏幕上仍然没有显示任何东西。

下面是从CGPathRef获取NSBezierPath的代码

代码语言:javascript
复制
- (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.7SDK,不能使用内置的CGPath方法,该方法添加了新的NSBezierPath SDK。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-03 12:37:12

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

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17427759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档