旋转90度后,如何将图像添加到CALayer背面?

问题描述:

我想在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

据我所知,这样做最简单的方法是添加代表正面和页面的背面两个子层。将两个子层'doubleSided属性设置为NO。将背面图层转换设置为您正在动画的相同翻转变换。然后,当页面的正面朝向观看者时,正面层可见并且背面层“隐藏”,反之亦然。

请参阅GeekGameBoard源代码(Card.m)的技术样本。

+0

谢谢你的提示。我最终同时制作了两层动画,并且当程度高于90时,背层会自动到达前面。我没有触及doubleSided属性。谢谢你们一样。 – nonamelive 2010-12-21 14:59:52