Ios 我如何知道动画组中的当前动画?

Ios 我如何知道动画组中的当前动画?,ios,objective-c,core-animation,Ios,Objective C,Core Animation,现在我想使用核心动画编写一些动画方法,就像Cocos2d一样简单,就像ccMove,ccFade,ccRotate 一切似乎都正常,但当我想实现动画序列时,我遇到了麻烦。我想到的第一件事是CCAnimationGroup,但是当动画成组时,我不知道当前动作何时完成,所以我无法设置层的属性,这导致层恢复到原始状态。有人知道吗?我的代码: UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

现在我想使用核心动画编写一些动画方法,就像Cocos2d一样简单,就像
ccMove
ccFade
ccRotate

一切似乎都正常,但当我想实现动画序列时,我遇到了麻烦。我想到的第一件事是
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];
你可以运行它,你会看到文本视图的位置和旋转不是我想要的


有人能帮我吗

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


-(void)animation didstop:(CAAnimation*)动画完成:(BOOL)标志

我已经解决了。事实上,我不需要知道组中的一个动作何时完成。只需设置动画的填充模式移除完成,这样图层就不会回到原始状态

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。这就行了。我还发现scaleAnimationfadeAnimation应该将它们的fillmode设置为kcafillmode两者,这样图层就不能恢复到原始属性状态


你也可以在我的github上查看代码。谢谢大家!

我把代码放在github上。希望你能看到它并给我帮助。谢谢大家!@你说你已经解决了这个问题?如果你自己解决了它,你可以写下你的解决方案并接受它。顺便说一下,我也是中国人。谢谢你提醒我!我尝试制作核心动画方法,比如cocos2d的动作。你的方法是对的,但是如果使用委托,对我来说会更麻烦。我们也可以使用block,就像:[CATransaction begin];[CATTransaction setDisableActions:是];[CATTransaction setCompletionBlock:^{NSLog(@“动画完成”);};[tTarget.layer addAnimation:action-forKey:nil];[CATransaction-commit];有趣的是,我们所有关注这个问题的人都是中国人?我也是。@Danny Xu,也许是中国英语。哈哈