我一直在尝试使用 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"];
我有两个问题:
- 复制层从终点开始。
- 当主层到达他的最后一个动画点时,动画停止并且复制层无法完成它们的动画。
最佳答案
为动画对象的 fillMode 和 removedOnCompletion 属性设置正确的值有望解决您的问题:
"The replicated layers are starting in the End point."
这是由分配给 instanceDelay
属性的值引起的,它在您的示例中将动画延迟了 0.15 秒。要从 fromValue 呈现动画,您需要通过将动画的 fillMode 设置为 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."
发生这种情况是因为默认情况下“动画在完成后从目标图层的动画中移除”。 1 .您可以通过将动画属性 removedOnCompletion 设置为 NO 来覆盖此默认行为。
animation.removedOnCompletion = NO;
希望这对您有所帮助。 :)
关于iphone - 使用 CAReplicatorLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7582963/