我有一个动画,我想将它添加到两个层:
[view1.layer addAnimation:theAnimation
forKey:@"layer1_animation"];
[view2.layer addAnimation:theAnimation
forKey:@"layer2_animation"];
但是当我运行我的应用程序时,只有第1个视图是动画。正如documentation所述,图层复制其动画:
此对象由渲染树复制,未引用。因此,对对象的后续修改不会传播到渲染树中。
所以我的代码应该正常工作。这是一个错误还是我做错了?
更多代码:
CGFloat currentY = self.view1.layer.position.y;
CABasicAnimation *yAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
yAnimation.fromValue = @(currentY - 2.f);
yAnimation.toValue = @(currentY + 2.f);
yAnimation.duration = 5.0;
yAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
yAnimation.repeatCount = HUGE_VALF;
yAnimation.autoreverses = YES;
yAnimation.fillMode = kCAFillModeBackwards;
[self.view1.layer addAnimation:yAnimation
forKey:@"view1_y_animation"];
[self.view2.layer addAnimation:yAnimation
forKey:@"view2_y_animation"];
答案 0 :(得分:0)
问题出在我的代码中。当从动画的Value和toValue初始化时,我使用了view1的y位置。所以view1覆盖了view2,它是不可见的。