2010-12-20 119 views
3

我想在CALayer的旋轉變換度高於90時添加背景圖像。旋轉90度後,如何將圖像添加到CALayer背面?

它就像Flipboard翻轉動畫一樣。

這是我當前的代碼:

CALayer *layer = self.view.layer; 

int frames = 130; 

layer.anchorPoint = CGPointMake(0, 0.5); 

CATransform3D transform = CATransform3DIdentity; 
transform.m34 = 1.0/-2000.0; 
transform = CATransform3DRotate(transform, -frames * M_PI/180.0, 0.0, 1.0, 0.0); 

self.view.layer.transform = transform; 

CAKeyframeAnimation *pivot = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; 

NSMutableArray *values = [[NSMutableArray alloc] initWithCapacity:45]; 
NSMutableArray *times = [[NSMutableArray alloc] initWithCapacity:45]; 

for (int i = 0; i < frames; i++) { 

    CATransform3D transform = CATransform3DIdentity; 
    transform.m34 = 1.0/-2000.0; 
    transform = CATransform3DRotate(transform, -M_PI/180.0 * i, 0.0, 1.0, 0.0); 

    [values addObject:[NSValue valueWithCATransform3D:transform]]; 
    [times addObject:[NSNumber numberWithFloat:(float)i/(frames - 1)]]; 
} 

pivot.values = values; 
pivot.keyTimes = times; 

[values release]; 
[times release]; 

pivot.duration = 0.5f; 
pivot.calculationMode = kCAAnimationLinear; 

[layer addAnimation: pivot forKey: @"pivot"]; 

誰能告訴我如何添加回圖像就像翻頁效果。

alt text

回答

3

據我所知,這樣做最簡單的方法是添加代表正面和頁面的背面兩個子層。將兩個子層'doubleSided屬性設置爲NO。將背面圖層轉換設置爲您正在動畫的相同翻轉變換。然後,當頁面的正面朝向觀看者時,正面層可見並且背面層「隱藏」,反之亦然。

請參閱GeekGameBoard源代碼(Card.m)的技術樣本。

+0

謝謝你的提示。我最終同時製作了兩層動畫,並且當程度高於90時,背層會自動到達前面。我沒有觸及doubleSided屬性。謝謝你們一樣。 – nonamelive 2010-12-21 14:59:52