如何知道CAAnimationGroup中的当前动画?

时间:2013-12-25 03:16:07

标签: ios objective-c core-animation

现在我想使用Core Animation为动画创建一些方法,就像Cocos2d一样简单,就像ccMoveccFadeccRotate一样。

一切似乎都没问题,但是当我想要实现动画序列时,我一直都很困扰。我首先想到的是CCAnimationGroup,但是当组中的动画时,我无法知道当前的动作完成后,我无法设置图层的属性,这导致图层恢复到原始状态。谁知道这个?我的代码:

    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    testView.backgroundColor= [UIColor redColor];
    testView.center = self.view.center;
    //mark1: textview's position is self.view.center
    [self.view addSubview:testView];

    CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation 
        animationWithKeyPath:@"position"];
    moveAnimation.duration = 3;
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 100, 100);
    CGPathAddLineToPoint(path, NULL, 200, 200);
    moveAnimation.path = path;
    //mark2: I want the testview move to CGPointMake(100,200)
    CGPathRelease(path);   

    CABasicAnimation *angleAnimation = 
        [CABasicAnimation 
          animationWithKeyPath:@"transform.rotation"];
    angleAnimation.toValue = @(45 * M_PI / 180);
    angleAnimation.duration = 3;
    angleAnimation.beginTime = 3;  //make3: rotate testview 45°

    CAAnimationGroup *actionGroup = [CAAnimationGroup animation];
    [actionGroup setDuration:6];
    [actionGroup setAnimations:@[moveAnimation, angleAnimation]];

    [CATransaction begin];

    [testView.layer addAnimation:actionGroup forKey:nil];

    [CATransaction commit];

你可以运行它,你会看到textview的位置和旋转不是我想要的。

有人能帮助我吗?

2 个答案:

答案 0 :(得分:1)

我已经解决了它。实际上,我不需要知道群组中的某个动作何时完成。只需设置动画 fillMode 和< strong> removedOnCompletion ,这样该图层就不会回到原始状态。

UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
testView.backgroundColor= [UIColor redColor];
testView.center = self.view.center;
[self.view addSubview:testView];   //mark1: textview's position is self.view.center

CAKeyframeAnimation *moveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveAnimation.duration = 3;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 100, 100);
CGPathAddLineToPoint(path, NULL, 200, 200);
moveAnimation.path = path;
moveAnimation.fillMode = kCAFillModeForwards;
moveAnimation.removedOnCompletion = NO;
CGPathRelease(path);    //mark2: I want the testview move to CGPointMake(100,200)

CABasicAnimation *angleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
angleAnimation.toValue = @(45 * M_PI / 180);
angleAnimation.duration = 3;
angleAnimation.beginTime = 3;  //make3: rotate testview 45°
angleAnimation.fillMode = kCAFillModeForwards;
angleAnimation.removedOnCompletion = NO;

CAAnimationGroup *actionGroup = [CAAnimationGroup animation];
[actionGroup setDuration:6];
[actionGroup setAnimations:@[moveAnimation, angleAnimation]];
actionGroup.fillMode = kCAFillModeForwards;
actionGroup.removedOnCompletion = NO;

[CATransaction begin];

[testView.layer addAnimation:actionGroup forKey:nil];

[CATransaction commit];

在上面的代码中,我将所有操作的fillmode设置为 kCAFillModeForwards ,并且 removedOnCompletion = NO 。这只是工作。我还发现 scaleAnimation fadeAnimation 应将其fillmode设置为 kCAFillModeBoth ,以便图层无法恢复到原始属性状态。

您也可以查看我的github上的代码。https://github.com/Vienta/SSAnimation谢谢!

答案 1 :(得分:0)

CAAnimationGroup是CAAnimation的子类,因此您可以拥有CAAnimationGroup的委托,并在动画停止时接收通知。

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag