UiView PopOut的Jerky Animation

时间:2013-07-29 10:51:44

标签: iphone objective-c core-animation quartz-core

我有一个UIView,我正在使用以下代码进行弹出式动画

float duration=1.3f;
CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
scale.duration = duration;
scale.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:.5f],
                [NSNumber numberWithFloat:1.1f],
                [NSNumber numberWithFloat:0.95f],
                [NSNumber numberWithFloat:1.f],
                nil];

CABasicAnimation *fadeIn = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeIn.duration = duration * .2f;
fadeIn.fromValue = [NSNumber numberWithFloat:0.f];
fadeIn.toValue = [NSNumber numberWithFloat:1.f];
fadeIn.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
fadeIn.fillMode = kCAFillModeForwards;

CAAnimationGroup *animationgroup = [CAAnimationGroup animation];
animationgroup.delegate=self;
animationgroup.animations = [NSArray arrayWithObjects:scale, fadeIn, nil];
animationgroup.duration = duration;
animationgroup.fillMode = kCAFillModeForwards;
animationgroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[animationgroup setValue:@"popIn" forKey:@"name"];
animationgroup.fillMode=kCAFillModeForwards;

[self.view.layer addAnimation:animationgroup forKey:@"popIn"];

这很有效。它以完美的方式在视图中弹出。但弹出动画的代码在动画结束时给出一个看起来非常奇怪的混蛋。

这是我的弹出代码

float duration=0.15f;
CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
scale.duration = duration;
scale.removedOnCompletion = NO;
scale.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:1.f],
                [NSNumber numberWithFloat:1.1f],
                [NSNumber numberWithFloat:.15f],
                nil];

CABasicAnimation *fadeOut = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeOut.duration = duration * .4f;
fadeOut.fromValue = [NSNumber numberWithFloat:1.f];
fadeOut.toValue = [NSNumber numberWithFloat:0.f];
fadeOut.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
fadeOut.beginTime = duration * .6f;
fadeOut.fillMode = kCAFillModeBoth;


CAAnimationGroup *animationgroup = [CAAnimationGroup animation];
animationgroup.animations = [NSArray arrayWithObjects:scale, fadeOut, nil];
animationgroup.duration = duration;
animationgroup.fillMode = kCAFillModeForwards;
animationgroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[animationgroup setValue:@"popOut" forKey:@"name"];
animationgroup.fillMode=kCAFillModeForwards;


[self.view.layer addAnimation:animationgroup forKey:@"popOut"];

2 个答案:

答案 0 :(得分:2)

比例序列1.0, 1.1, 0.15 应该导致混蛋。

如果您对视觉效果不满意,请尝试摆弄上面的值和计时功能(您使用的是“easeIn”而不是“easeInOut”)。

答案 1 :(得分:1)

在动画期间,只有视图层的模态图层保持其原始状态时,表示层才会发生变化。 在您的情况下,您在动画完成时不保留表示层的状态,并且出现原始形式的Modal图层。这似乎是Jerk效应。

在“animationgroup.fillMode = kCAFillModeForwards”之后使用以下行:保留表示层状态,抽搐就会消失。

animationgroup.removedOnCompletion=NO;