我发现了一种实现我的结果的方法。我不确定这是最好的方法,所以仍然会感谢其他想法。
我同时创建 layer1 和 layer2 并将 Sublayer 都添加到 view.layer。然后我为 layer1 设置动画以将其移动到新位置,持续时间 = 0.3。对于 layer2,我使用一个由移动(beginTime = 0.3,duration = 0.3)和另一个使用 hidden 属性的动画组成的组进行动画处理。后一个动画立即隐藏 layer2,然后在 beginTime = 0.3 时取消隐藏。为了实现后一个动画,我使用了 CAKeyFrameAnimation,因为这个动画必须是离散的——要么完全隐藏,要么完全不隐藏。
这是代码。我通过删除有关我的实现的细节来简化它,所以这不是我的应用程序中的实际代码——如果有任何错误,我们深表歉意。
CALayer layer1 = [CALayer layer];
CALayer layer2 = [CALayer layer];
/*
* set attributes of the layers -- cut out of this example
*/
layer1.position = toPosition1; // positions when animation is complete
layer2.position = toPosition2;
[myView.layer addSublayer:layer1];
[myView.layer addSublayer:layer2];
// animate layer 1
CABasicAnimation* move1 = [CABasicAnimation animationWithKeyPath:@"position"];
move1.duration = 0.3;
move1.fromValue = fromPosition2;
[layer1 addAnimation:move1 forKey:nil];
// animate layer 2 with a group
CABasicAnimation* move2 = [CABasicAnimation animationWithKeyPath:@"position"];
move2.duration = 0.3;
move2.fromValue = fromPosition2;
move2.beginTime = 0.3;
CAKeyframeAnimation *show = [CAKeyframeAnimation animationWithKeyPath:@"hidden"];
show.values = [NSArray arrayWithObjects:[NSNumber numberWithBool:YES], [NSNumber numberWithBool:NO], [NSNumber numberWithBool:NO], nil];
// times are given as fractions of the duration time -- hidden for first 50% of 0.6 sec
show.keyTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:1.0], nil];
show.calculationMode = kCAAnimationDiscrete;
show.duration = 0.6;
show.beginTime = 0;
CAAnimationGroup *grp = [CAAnimationGroup animation];
[grp setAnimations:[NSArray arrayWithObjects:move2, show, nil ]];
grp.duration = 0.6;
[layer2 addAnimation:grp forKey:nil];
我