多个CAKeyframeAnimation同时在不同的层中 [英] multiple CAKeyframeAnimation in different layers at the same time

查看:71
本文介绍了多个CAKeyframeAnimation同时在不同的层中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在有谁可以使用CAKeyframeAnimation同时为多个图层设置动画效果?
每个图层都有自己的CAKeyframeAnimation对象。看看下面的代码:

Does anyone now how can I animate multiple layers at the same time using CAKeyframeAnimation ? Each layer has its own CAKeyframeAnimation object. Take a look at the code below:

我有一个接收对象的方法,创建CAKeyframeAnimation并将动画附加到它:

I have a method that receives an object, creates the CAKeyframeAnimation and attaches the animation to it:

- (void)animateMovingObject:(CALayer*)obj
               fromPosition:(CGPoint)startPosition
                 toPosition:(CGPoint)endPosition
                   duration:(NSTimeInterval)duration {
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    //pathAnimation.fillMode = kkCAFillModeRemoved; // default 
    //pathAnimation.removedOnCompletion = YES; // default
    pathAnimation.duration = duration;

    // create an empty mutable path
    CGMutablePathRef curvedPath = CGPathCreateMutable();

    // set the starting point of the path
    CGPathMoveToPoint(curvedPath, NULL, startPosition.x, startPosition.y);

    CGPathAddCurveToPoint(curvedPath, NULL, 
                          startPosition.x, endPosition.y, 
                          startPosition.x, endPosition.y,
                          endPosition.x, endPosition.y);
    pathAnimation.path = curvedPath;
    [obj addAnimation:pathAnimation forKey:@"pathAnimation"];
    CGPathRelease(curvedPath);
}

现在,假设我在棋盘游戏中添加了3个图层作为子图层我拨打以下电话:

Now, suppose I have 3 layers added as a sublayer in my board game and I make the following calls:

CALayer obj1 = ... // set up layer and add as sublayer
[self animateMovingObject:obj1
             fromPosition:CGPointMake(0.0, 0.0)
               toPosition:CGPointMake(100.0, 100.0)
                 duration:2.0];

CALayer obj2 = ... // set up layer and add as sublayer
[self animateMovingObject:obj2
             fromPosition:CGPointMake(0.0, 0.0)
               toPosition:CGPointMake(150.0, 100.0)
                 duration:2.0];

CALayer obj3 = ... // set up layer and add as sublayer
[self animateMovingObject:obj3
             fromPosition:CGPointMake(0.0, 0.0)
               toPosition:CGPointMake(200.0, 100.0)
                 duration:2.0];

通过这样做,我只能看到 obj3 从位置(0.0,0.0)移动到(200.0,100.0)。
我缺少什么?我应该使用NSOperationQueue / Threads吗?
在此上下文中使用CAKeyframeAnimation的 animationDidStart:委托方法似乎没有用。

By doing this, I can see only the obj3 being moved from position (0.0, 0.0) to (200.0, 100.0). What am I missing? Should I use NSOperationQueue/Threads? Using the animationDidStart: delegate method of the CAKeyframeAnimation doesn't seem to be useful in this context.

有任何想法吗?

提前致谢。

推荐答案

如果你想要对这样的一系列动画进行分组,以便保证它们被同步,你可以将它们包装在一个CATransaction中:

If you want to group a series of animations like this, so that they are guaranteed to be synchronized, you could wrap them in a CATransaction:

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:2.0] forKey:kCATransactionAnimationDuration];     

// Do animations

[CATransaction commit];     

但是,我不太清楚为什么你写的代码不会触发每个图层的动画都是正确的,即使它们没有同步也是如此。

However, I'm not quite sure why the code you've written doesn't fire off the correct animations for each layer, even if they weren't synchronized.

注意,通过将 removedOnCompletion 设置为YES,您的动画将运行,然后对象将跳到最后的起点。你可能希望那是NO。

Note that by setting removedOnCompletion to YES, your animation will run and then the object will hop back to its starting point at the end. You probably want that to be NO.

这篇关于多个CAKeyframeAnimation同时在不同的层中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆