在CAKeyframeAnimation之后,UIImageView正在消失 [英] UIImageView is disappearing after CAKeyframeAnimation

查看:283
本文介绍了在CAKeyframeAnimation之后,UIImageView正在消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying this code (found in an answer here on SO) in a category on UIView and am using it to peform a "pop" animation on a UIImageView nested inside of a UITableViewCell.

- (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"];
}

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

The animation works properly, but when it finishes, it disappears. I've found answers to others with this problem, but apparently setting the fillMode will not work in this instance.

推荐答案

Don'使用这两行。而是实际上在图层上设置变换。替换这两行:

Don't use those two lines. Instead actually set the transform on the layer. Replace these two lines:

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

最终转换状态:

[[self layer] setTransform:scale4];

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

When setting fill mode forwards and not removing the animation, it is causing the final state to be visual only. You need to actually change the layer's internal state for the property you're animating. To do so, override the animation for the transform property by using the transform name when adding the animation to the layer. This will cause the animation to use your animation instead of the default.

[[self layer] addAnimation:animation forKey:@"transform"];

这篇关于在CAKeyframeAnimation之后,UIImageView正在消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆