2012-03-30 119 views
2

我動畫(移動)一個子類的UIImageView像這樣:動畫UIView - 如何跟蹤位置?

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration: 1]; 
[UIView setAnimationDelay:0]; 
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
if (position == 0) position = 1; 
if (position > 6) position--;  
self.transform = CGAffineTransformMakeTranslation(position*120, 0); 
[UIView commitAnimations]; 

然後,我想讓它脈動以突出顯示它:

CAKeyframeAnimation *bounce = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; 
CATransform3D forward = CATransform3DMakeScale(1.3, 1.3, 1); 
CATransform3D back = CATransform3DMakeScale(0.7, 0.7, 1); 
CATransform3D forward2 = CATransform3DMakeScale(1.2, 1.2, 1); 
CATransform3D back2 = CATransform3DMakeScale(0.9, 0.9, 1); 
[bounce setValues:[NSArray arrayWithObjects: 
        [NSValue valueWithCATransform3D:CATransform3DIdentity], 
        [NSValue valueWithCATransform3D:forward], 
        [NSValue valueWithCATransform3D:back], 
        [NSValue valueWithCATransform3D:forward2], 
        [NSValue valueWithCATransform3D:back2], 
        [NSValue valueWithCATransform3D:CATransform3DIdentity], 
        nil]]; 
[bounce setDuration:0.6]; 
[[super layer] addAnimation:bounce forKey:@"bounceanimation"]; 

這所有的作品。它被120像素移動,但是當打電話給脈衝碼時,它會在原來的位置上跳動,而不是我剛纔移動的位置。爲什麼是這樣的,我該如何改變它?

tobias

回答

1

好,這是你的問題:

self.transform = CGAffineTransformMakeTranslation(position*120, 0); 

請注意,您是翻譯視圖到另一個地方,但像任何變換,對象的原始統計數據保持不變。

您只需將該代碼轉換爲setFrame:方法,以便實際移動圖像。

+0

終於明白了!謝謝!把它改成[self setFrame:CGRectMake(30 + position * 120,self.frame.origin.y,self.frame.size.width,self.frame.size.height)]; – 2012-03-30 18:19:08

+0

沒問題!樂於幫助! – David 2012-03-30 22:06:49

0

別擔心!只需加上這個

bounce.fillMode = kCAFillModeForwards; 

它會讓你的動畫停留在終點。