2012-01-01 118 views
1

下面是我使用的代碼。動畫的作品。不過,之後它又回到了原來的狀態。我的代碼有什麼問題嗎?謝謝。iphone - 動畫跳回到原始狀態

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    CABasicAnimation *expand=[CABasicAnimation animationWithKeyPath:@"transform"]; 
    expand.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]; 
    expand.autoreverses=NO; 
    expand.removedOnCompletion=YES; 

    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
    opacityAnimation.toValue = [NSNumber numberWithFloat:0.0f]; 
    opacityAnimation.autoreverses=NO; 
    opacityAnimation.removedOnCompletion=YES; 

    CAAnimationGroup *group=[CAAnimationGroup animation]; 
    group.animations=[[NSArray alloc] initWithObjects:expand,opacityAnimation, nil]; 
    group.duration=1.0; 
    group.fillMode=kCAFillModeForwards; 
    group.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
    [view1.layer addAnimation:group forKey:@"expand"]; 

} 

回答

1

要設置removedOnCompletionYES它指定完成後動畫將被刪除。改爲嘗試NO

+0

仍然不起作用 – 2012-01-01 13:12:49

0

一個簡單的方法來擺脫跳回的是使用CATransaction沒有行動,明確地更新,你是動畫的數值模型層啓用:

[CATransaction begin]; 
[CATransaction setDisableActions:YES]; 

//this will update the model layer 
view1.layer.transform = CATransform3DMakeScale(1.2, 1.2, 1.0); 
view1.layer.opacity = 0.f; 

//your basic animations will animate the presentation layer 
/* your CABasicAnimations here */ 

[CATransaction commit]; 

不要設置removedOnCompletionNO以保持視圖到位,因爲這將使動畫永遠持續下去,並且模型層將永遠不會更新表示層顯示的位置。由於GPU正在動畫一個並非真正移動的視圖,因此您可能會因爲從不移除動畫而導致性能損失。

0
CAAnimationGroup *group=[CAAnimationGroup animation]; 
group.animations=[[NSArray alloc] initWithObjects:expand,opacityAnimation, nil]; 
group.duration=1.0; 
group.removedOnCompletion=NO; 
group.fillMode=kCAFillModeForwards; 
group.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
[view1.layer addAnimation:group forKey:@"expand"]; 

removedOnCompletion爲NO組動畫它可能爲你工作嘗試。

相關問題