【问题标题】:UIImageView is disappearing after CAKeyframeAnimationCAKeyframeAnimation 后 UIImageView 消失
【发布时间】:2011-04-04 15:40:08
【问题描述】:

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

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

【问题讨论】:

    标签: iphone core-animation uiimageview calayer caanimation


    【解决方案1】:

    不要使用这两行。相反,实际上在图层上设置了变换。替换这两行:

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

    最终的变换状态:

    [[self layer] setTransform:scale4];
    

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

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

    【讨论】:

    • 这是错字吗? [[self layer] setTransform:[NSValue valueWithCATransform3D:scale4]]; 抛出一个语法错误,提示“没有从 NSValue 到 CATransform3D 的可行转换”。当然 NSValue 包装应该只在设置 fromValue 和 toValue 时使用?
    • 确实如此。不知道为什么这么长时间没有被注意到。谢谢。现已修复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多