2011-09-23 60 views
1

我的堆棧溢出:)時間設置與CATransform3DMakeTranslation

第一個問題,我想打一個CALayer的y軸平移。我有一個很大的背景:320x4000px。 翻譯正在與下面的代碼:

NSNumber *deplacement = [NSNumber numberWithFloat:([app.session.progression floatValue] * HAUTEUR_FOND) /100]; 

self.backgroundLayer.transform = CATransform3DMakeTranslation(0, -[deplacement floatValue], 0); 

但有了這個代碼,這是不可能建立一個持續時間...

我試圖與:

CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"]; 
transformAnimation.duration = 5.0f; 
transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, -[deplacement floatValue], 0)]; 
[self.backgroundLayer addAnimation:transformAnimation forKey:@"position.y"]; 

但不起作用...

感謝您的幫助:)

回答

2

好的,這裏是答案,也許它會幫助別人:)

NSNumber *deplacement = [NSNumber numberWithFloat:([app.session.progression floatValue] * HAUTEUR_FOND) /100]; 
DebugLog(@"deplacement : %@", deplacement); 

if(!backgroundPositionMemo) backgroundPositionMemo = 0; 

CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
transformAnimation.duration = 1.0f; 
transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, -backgroundPositionMemo, 0)]; 
transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, -[deplacement floatValue], 0)]; 
transformAnimation.removedOnCompletion = NO; 
transformAnimation.fillMode = kCAFillModeForwards; 
[self.backgroundLayer addAnimation:transformAnimation forKey:@"transform"]; 

backgroundPositionMemo = [deplacement floatValue]; 
+0

非常感謝!我在類似的代碼中使用keyPath「origin.x」而不是「transform」,並且你的代碼指向了正確的方向! – iago849