我使用 CAKeyframeAnimations 来动画化一个图层的 transform.rotation 和 transform.translation.x 属性,但我尝试使用隐式动画来动画化 transform 属性时出现了问题。我有一个图层必须在两个状态之间进行动画,并且 CABasicAnimation 的默认插值完全不正确,无法按照我想要的路径进行运动。CAKeyframeAnimation 看起来能解决这个问题,但实际上并非如此。任何尝试使用 CAKeyframeAnimation 动画化 transform 的操作都会导致视图立即跳转到最终的变换状态,而其他动画则继续运行。如果我删掉下面这个函数的前半部分,并让我的 "transform" 事件在底部使用 CABasicAnimation,那么它就可以正常地进行动画 - 尽管在一些错误的插值变换过程中。
我的图层委托实现了以下内容:
- (id <CAAction>) actionForLayer:(CALayer *)layer forKey:(NSString *)event
{
if ([event isEqualToString:@"transform"])
{
CGSize startSize = ((CALayer *)self.layer.presentationLayer).bounds.size;
CGSize endSize = self.layer.bounds.size;
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:event];
animation.duration = 0.25;
NSMutableArray *values = [NSMutableArray array];
int stepCount = 10;
for (int i = 0; i < stepCount; i++)
{
CGFloat p = i / (float)(stepCount - 1);
CGSize size = [self interpolateBetweenSize:startSize andSize:endSize percentage:p];
CATransform3D transform = [self transformForSize:size];
[values addObject:[NSValue valueWithCATransform3D:transform]];
}
animation.values = values;
return animation;
}
// All other animations use this basic animation
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:event];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.removedOnCompletion = YES;
animation.fillMode = kCAFillModeForwards;
animation.duration = 0.25;
return animation;
}
我的变换是先进行平移再进行旋转,但我认为使用一组动画和单独的关键帧动画来通过平移和旋转进行动画处理会导致混乱。我已确认对于我传递的所有p值,大小和变换都是正确的,并且p严格地从0到1范围内。
我尝试设置非默认的timing function,尝试设置一个timing function数组,省略了keyTimes,设置了重复次数为0(repeatCount),removedOnCompletion=YES,以及fillMode为forwards,但这些都没有效果。我是否没有正确创建变换关键帧动画?
nil
。 - Paul.s