UIImageView正在消失后CAKeyframeAnimation

问题描述:

我想在UIView类别上的这个代码(在这里回答中找到),并使用它来执行嵌套在UITableViewCell内的UIImageView的“流行”动画。UIImageView正在消失后CAKeyframeAnimation

- (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不起作用。

请勿使用这两行。相反,实际上在图层上设置了转换。与最终变换状态

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

::替换这两行

[[self layer] setTransform:scale4]; 

当设置填充模式向前和不去除动画,它导致最终状态是视觉仅。你需要实际改变你的动画属性的图层内部状态。为此,在将动画添加到图层时,通过使用转换名称来覆盖transform属性的动画。这将导致动画使用您的动画而不是默认值。

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

这是排课吗? '[[self layer] setTransform:[NSValue valueWithCATransform3D:scale4]];'抛出语法错误,说'没有可行的从NSValue到CATransform3D的转换'。当设置fromValue和toValue时,只能使用NSValue包装? – Echelon 2015-04-17 10:14:03

+0

确实。不知道如何不被忽视这么久。谢谢。现在修复。 – 2015-04-18 17:09:21