在CAKeyframeAnimation之后,UIImageView正在消失

Mar*_*ams 4 iphone core-animation calayer uiimageview caanimation

我在UIView上的一个类别中尝试使用此代码(在SO的答案中找到),并使用它在嵌套在UITableViewCell内的UIImageView上执行"pop"动画.

- (void)attachPopUpAnimation
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation
       animationWithKeyPath:@"transform"];

    CATransform3D scale1 = CATransform3DMakeScale(0.5, 0.5, 1);
    CATransform3D scale2 = CATransform3DMakeScale(1.2, 1.2, 1);
    CATransform3D scale3 = CATransform3DMakeScale(0.9, 0.9, 1);
    CATransform3D scale4 = CATransform3DMakeScale(1.0, 1.0, 1);

    NSArray *frameValues = [NSArray arrayWithObjects:
       [NSValue valueWithCATransform3D:scale1],
       [NSValue valueWithCATransform3D:scale2],
       [NSValue valueWithCATransform3D:scale3],
       [NSValue valueWithCATransform3D:scale4],
       nil];
    [animation setValues:frameValues];

    NSArray *frameTimes = [NSArray arrayWithObjects:
         [NSNumber numberWithFloat:0.0],
         [NSNumber numberWithFloat:0.5],
         [NSNumber numberWithFloat:0.9],
         [NSNumber numberWithFloat:1.0],
         nil];    
    [animation setKeyTimes:frameTimes];

    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    animation.duration = 0.2;

    [self.layer addAnimation:animation forKey:@"popup"];
}
Run Code Online (Sandbox Code Playgroud)

动画工作正常,但一旦完成,它就会消失.我已经找到了解决其他问题的答案,但显然设置fillMode在这个实例中不起作用.

Mat*_*ong 11

不要使用这两行.而是实际在图层上设置变换.替换这两行:

animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
Run Code Online (Sandbox Code Playgroud)

最终转换状态:

[[self layer] setTransform:scale4];
Run Code Online (Sandbox Code Playgroud)

当向前设置填充模式而不是移除动画时,它导致最终状态仅是可视的.您需要实际更改要为其设置动画的属性的图层内部状态.为此,请在将动画添加到图层时使用变换名称覆盖transform属性的动画.这将导致动画使用您的动画而不是默认动画.

[[self layer] addAnimation:animation forKey:@"transform"];
Run Code Online (Sandbox Code Playgroud)