2013-07-29 74 views
2

我有一個UIView爲此我使用了流行的動畫生澀的動畫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"]; 

下面的代碼工作Fone公司。它以完美的方式在視圖中彈出。但彈出式動畫的代碼在動畫結束時給出了一個看起來相當奇怪的動作。

這裏是我的彈出式視窗代碼

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"]; 
+0

模擬器或設備?有時模擬器上的動畫看起來不太好。 – Mundi

回答

1

在動畫過程中,只有表示層在視圖的模態層保持其原始狀態時纔會發生變化。 在你的情況下,當動畫完成時,你不會保留表示層的狀態,並且Modal層出現在它的原始形式中。這看起來像是Jerk效應。

在「animationgroup.fillMode = kCAFillModeForwards;」之後使用以下行保持表示層狀態並且挺舉將消失。

animationgroup.removedOnCompletion=NO; 
+0

那就是我在找的東西。完美的答案。謝謝。 – NaXir

2

規模序列1.0, 1.1, 0.15應該導致混蛋。

如果您對視覺效果不滿意,請嘗試擺弄上面的值和計時功能(您正在使用「easeIn」而不是「easeInOut」)。

+0

這聽起來不對。這如何影響「動畫的開始」? – Mundi