2011-09-14 57 views
3

在iOS中,我試圖創建縮小圖標的效果,並在淡出時跨過屏幕在弧中飛行,然後消失。我已經用CAAnimationGroup實現了這3種效果,並且它符合我的要求。問題在於動畫結束時,視圖出現在原始位置,全尺寸和完全不透明。任何人都可以看到我在下面的代碼中做錯了什麼? 動畫不應該恢復到原來的位置,而是在最後消失。CAAnimationGroup在完成時恢復到原始位置

UIBezierPath *movePath = [UIBezierPath bezierPath]; 
CGPoint libraryIconCenter = CGPointMake(610, 40); 

CGPoint ctlPoint = CGPointMake(self.imgViewCropped.center.x, 22.0); 
movePath moveToPoint:self.imgViewCropped.center]; 
[movePath addQuadCurveToPoint:libraryIconCenter 
       controlPoint:ctlPoint]; 

CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
moveAnim.path = movePath.CGPath; 
moveAnim.removedOnCompletion = NO; 

CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"]; 
scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 
scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]; 
scaleAnim.removedOnCompletion = NO; 

CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"]; 
opacityAnim.fromValue = [NSNumber numberWithFloat:1.0]; 
opacityAnim.toValue = [NSNumber numberWithFloat:0.0]; 
opacityAnim.removedOnCompletion = NO; 

CAAnimationGroup *animGroup = [CAAnimationGroup animation]; 
animGroup.animations = [NSArray arrayWithObjects:moveAnim,scaleAnim,opacityAnim, nil]; 
animGroup.duration = 0.6; 
animGroup.delegate = self; 
animGroup.removedOnCompletion = NO; 
[self.imgViewCropped.layer addAnimation:animGroup forKey:nil]; 

回答

7

我相信你需要你的動畫的fillMode屬性設置爲kCAFillModeForwards。這應該在結束時凍結動畫。另一個建議(老實說,這是我通常做的)就是在設置動畫之後,將圖層本身的屬性設置爲最終位置。通過這種方式,當動畫被移除時,該層將仍然具有作爲其模型的一部分的最終屬性。

另外,CAAnimationGroup中包含的removedOnCompletion動畫標誌將被忽略。您應該刪除這些作業,因爲它們有誤導性。按照上面的指定將它們替換爲fillMode

+0

感謝您的提示凱文 - 我已經按照建議設置了fillMode,但是當動畫結束時,UIImageView仍然會重新出現在它的原始位置。我在動畫塊之後添加了'self.imgViewCropped.layer.frame = CGRectMake(610,-40,40,40);',但它在動畫運行之前立即生效。 – Paul

+0

其實我的代碼是基於這個教程... [鏈接] http://nachbaur.com/blog/core-animation-part-3,但這遭受同樣的問題,圖像重新出現在原始baxck動畫結束時的位置。 – Paul

+1

@Paul:你試過設置'self.imgViewCropped.layer.position'嗎?它絕對不應該直接跳到那裏。您的動畫指定完整路徑,所以它不會將其從當前值中拉出來,並且動畫的位置值將覆蓋圖層嘗試使用的任何值。 –