gpt4 book ai didi

iphone - 如何成功地同时为多个 CALayer 制作动画?

转载 作者:行者123 更新时间:2023-12-03 19:44:44 27 4
gpt4 key购买 nike

我成功地对单个图层进行了动画处理,以改变其沿屏幕上任意路径的位置。我现在尝试多次复制此动画,以产生某种东西在拐角处弯曲的错觉。我将代码放入 CATransaction 中,并将其放入一个循环中,递增循环每次迭代的起始位置,然后在循环后提交 CATransaction。如果代码不在循环中(即,只有一层被动画化),那么在动画结束时所有层都会出现(在我的委托(delegate)在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 的红色正方形];

最佳答案

实际问题是每个层同时遵循相同的路径...解决方案是在延迟 (someSmallFractionOfTime * i) 后触发每个层/动画,其中 i 递增。

所以我将动画部分提取为新函数/方法/消息(无论它叫什么)

- (void) kickoffLaserAnimationWithPath: (NSArray *) path  {
CGPoint start = [(NSValue*)[path objectAtIndex:0] CGPointValue];
CALayer* laserLayer = [CALayer layer];
laserLayer.bounds = CGRectMake(start.x, start.y, 20, 10);
laserLayer.position = CGPointMake(start.x, start.y);
laserLayer.contents = (id)[UIImage imageNamed:@"Laser.png"].CGImage;
[self.layer addSublayer:laserLayer];

CAKeyframeAnimation* anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.values = path;
anim.duration = ([path count] * laserSpeed);
anim.removedOnCompletion = NO;
anim.delegate = self;
anim.rotationMode = kCAAnimationRotateAuto;
[anim setValue:@"Fire" forKey:@"Action"];
[anim setValue:laserLayer forKey:@"Layer"];
[laserLayer addAnimation:anim forKey:nil];
isAnimating = YES;

}

并在循环中调用它,如下所示:

NSArray* path = [board caclulatePath:s];
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:([path count] * laserSpeed)] forKey:kCATransactionAnimationDuration];
float numBetweenPoints = (float)((float)s.frame.size.height / (float)10) * 2;
float delay = (laserSpeed / numBetweenPoints);
for (int i = 0; i < [path count]; i++)
{
[self performSelector:@selector(kickoffLaserAnimationWithPath:) withObject:path afterDelay:(delay*i)];

}
[CATransaction commit];

瞧...

关于iphone - 如何成功地同时为多个 CALayer 制作动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6870398/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com