使用 CAReplicatorLayer

发布于 2024-12-07 03:25:20 字数 1323 浏览 2 评论 0原文

我一直在尝试使用 CAReplicatorlayer、CATextLayer 和非常基本的动画创建很酷的文本效果。我试图让这些字母看起来像是从屏幕顶部掉下来的,后面跟着很酷的复制器,它们会变得越来越不可见。我已经成功做到了这种效果,但并不完全。

到目前为止,这就是我所得到的:

CATextLayer *messageLayer = [CATextLayer layer];

[messageLayer setForegroundColor:[[UIColor blackColor] CGColor]];
[messageLayer setContentsScale:[[UIScreen mainScreen] scale]];
[messageLayer setFrame:CGRectMake(0, 0, 40, 40)];
[messageLayer setString:@"A"];


CAReplicatorLayer *replicatorX = [CAReplicatorLayer layer];

//Set the replicator's attributes
replicatorX.frame = CGRectMake(0, 0, 40, 40);
replicatorX.anchorPoint = CGPointMake(0,0);
replicatorX.position = CGPointMake(0, 0);
replicatorX.instanceCount = 9;
replicatorX.instanceDelay = 0.15;
replicatorX.instanceAlphaOffset = -0.1;

replicatorX.zPosition = 200;
replicatorX.anchorPointZ = -160;

[replicatorX addSublayer:messageLayer];

[self.view.layer addSublayer:replicatorX];


messageLayer.position = CGPointMake(40, 400);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];
animation.fromValue = [NSNumber numberWithInt:0];;
animation.toValue = [NSNumber numberWithInt:400];
animation.duration = 3;
[messageLayer addAnimation:animation forKey:@"s"];

我有两个问题:

  1. 复制的层从终点开始。
  2. 当主图层到达最后一个动画点时,动画会停止,并且复制图层无法完成其动画。

I've been trying to create a cool text effect with the CAReplicatorlayer, CATextLayer and very basic animations. I'm trying to make the letters look like they are being droped from the top of the screen followed by cool replicators which will become less and less visible. I've managed to do this effect but not completely.

So far this is what I've got:

CATextLayer *messageLayer = [CATextLayer layer];

[messageLayer setForegroundColor:[[UIColor blackColor] CGColor]];
[messageLayer setContentsScale:[[UIScreen mainScreen] scale]];
[messageLayer setFrame:CGRectMake(0, 0, 40, 40)];
[messageLayer setString:@"A"];


CAReplicatorLayer *replicatorX = [CAReplicatorLayer layer];

//Set the replicator's attributes
replicatorX.frame = CGRectMake(0, 0, 40, 40);
replicatorX.anchorPoint = CGPointMake(0,0);
replicatorX.position = CGPointMake(0, 0);
replicatorX.instanceCount = 9;
replicatorX.instanceDelay = 0.15;
replicatorX.instanceAlphaOffset = -0.1;

replicatorX.zPosition = 200;
replicatorX.anchorPointZ = -160;

[replicatorX addSublayer:messageLayer];

[self.view.layer addSublayer:replicatorX];


messageLayer.position = CGPointMake(40, 400);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.y"];
animation.fromValue = [NSNumber numberWithInt:0];;
animation.toValue = [NSNumber numberWithInt:400];
animation.duration = 3;
[messageLayer addAnimation:animation forKey:@"s"];

I have two problems:

  1. The replicated layers are starting in the End point.
  2. When the main layer reach his last animated point the animation stops and the replicated layers can't finish their animation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈独秀 2024-12-14 03:25:20

为动画对象的 fillMode 和 returnedOnCompletion 属性设置正确的值有望解决您的问题:

“复制的层从终点开始。”

这是由分配给 instanceDelay 属性的值引起的,在您的示例中该属性将动画延迟了 0.15 秒。要从 fromValue 呈现动画,您需要通过将动画的 fillMode 设置为 kCAFillModeBackwards 来解决此延迟。

animation.fillMode = kCAFillModeBackwards;

“当主层到达最后一个动画点时,动画停止
并且复制的图层无法完成其动画。”

发生这种情况是因为默认情况下“动画在完成后会从目标图层的动画中删除。”1。您可以通过将动画属性removedOnCompletion 设置为NO 来覆盖此默认行为。

animation.removedOnCompletion = NO;

希望这会有所帮助。

Setting the correct values to the animation object's fillMode and removedOnCompletion properties will hopefully solve your problems:

"The replicated layers are starting in the End point."

This is caused by the value assigned to instanceDelay property, which delays the animation with 0.15s in your example. To present the animation from the fromValue, you need to account for this delay by setting the animation's fillMode to kCAFillModeBackwards .

animation.fillMode = kCAFillModeBackwards;

"When the main layer reach his last animated point the animation stops
and the replicated layers could not finish their animation."

This happens because by default "the animation is removed from the target layer's animations upon completion."1. You can override this default behavior by setting the animation attribute removedOnCompletion to NO.

animation.removedOnCompletion = NO;

Hope this helps. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文