如何成功地同时为多个 CALayer 制作动画?
我成功地对单个图层进行了动画处理,以改变其沿屏幕上任意路径的位置。我现在尝试多次复制此动画,以产生某种东西在拐角处弯曲的错觉。我将代码放入 CATransaction 中,并将其放入一个循环中,递增循环每次迭代的起始位置,然后在循环后提交 CATransaction。如果代码不在循环中(即,只有一层被动画化),那么在动画结束时所有层都会出现(在我的委托在animationDidStop中的动画结束时删除它们之前),我看到的效果是相同的)
我编写的代码如下所示:
NSArray* path = [board caclulatePath:s];
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:([path count] * 0.25)] forKey:kCATransactionAnimationDuration];
for (int i = 0; i < 20; i++)
{
CALayer* laserLayer = [CALayer layer];
laserLayer.bounds = CGRectMake(s.frame.origin.x, s.frame.origin.y + (10*i), 20, 10);
laserLayer.position = CGPointMake(s.frame.origin.x + (s.frame.size.width / 2), s.frame.origin.y + (s.frame.size.height / 2) + (10*i));
laserLayer.contents = (id)[UIImage imageNamed:@"Laser.png"].CGImage;
[self.layer addSublayer:laserLayer];
CAKeyframeAnimation* anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.values = path;
anim.duration = ([path count] * 0.25);
anim.removedOnCompletion = NO;
anim.delegate = self;
anim.rotationMode = kCAAnimationRotateAuto;
[anim setValue:@"Fire" forKey:@"Action"];
[anim setValue:laserLayer forKey:@"Layer"];
[laserLayer addAnimation:anim forKey:nil];
}
[CATransaction commit];
其中 [board caclulatePath:s] 返回表示 CGPoints 的 NSValues 的 NSArray*。
我怎样才能达到我想要的效果(这是沿同一路径的laser.png的多个副本)? [Laser.png 是一个 20px x 20px 的红色正方形];
I'm successfully animating a single layer to alter its position along an arbitrary path on my screen. I'm now attempting to replicate this animation multiple times to give the illusion of something bending round a corner. I put the code inside a CATransaction and put it in a loop incrementing the starting position for each iteration of the loop, then committed the CATransaction after the loop. The effect I see is the same if the code was not in a loop (That is, just one layer being animated) then at the end of the animation all the layers appear (Before my delegate removes them at the end of the animation in animationDidStop)
The code I have written looks like:
NSArray* path = [board caclulatePath:s];
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:([path count] * 0.25)] forKey:kCATransactionAnimationDuration];
for (int i = 0; i < 20; i++)
{
CALayer* laserLayer = [CALayer layer];
laserLayer.bounds = CGRectMake(s.frame.origin.x, s.frame.origin.y + (10*i), 20, 10);
laserLayer.position = CGPointMake(s.frame.origin.x + (s.frame.size.width / 2), s.frame.origin.y + (s.frame.size.height / 2) + (10*i));
laserLayer.contents = (id)[UIImage imageNamed:@"Laser.png"].CGImage;
[self.layer addSublayer:laserLayer];
CAKeyframeAnimation* anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.values = path;
anim.duration = ([path count] * 0.25);
anim.removedOnCompletion = NO;
anim.delegate = self;
anim.rotationMode = kCAAnimationRotateAuto;
[anim setValue:@"Fire" forKey:@"Action"];
[anim setValue:laserLayer forKey:@"Layer"];
[laserLayer addAnimation:anim forKey:nil];
}
[CATransaction commit];
where [board caclulatePath:s] returns an NSArray* of NSValues representing CGPoints.
How can I achieve the effect I'm after (Which is multiple copies of laser.png following the same path)? [Laser.png is a 20px x 20px red square];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际问题是每个层同时遵循相同的路径...解决方案是在延迟 (someSmallFractionOfTime * i) 后触发每个层/动画,其中 i 递增。
因此,我将动画部分提取为新函数/方法/消息(无论它叫什么)
,并在循环中调用它,如下所示:
瞧...
The actual problem was that each layer WAS following the same path, at the same time... The solution was to fire off each Layer / Animation after a delay of (someSmallFractionOfTime * i) where i was incremented.
So I extracted the animation part as a new function / method / message (Whatever it's called)
and called it within the loop like so:
Voila...
看一下 CAAnimationGroup。我想这可能适合你的问题。
Take a look at CAAnimationGroup. I think it might suit your problem.