2010-02-25 37 views
1

我的对象上有两个UIImageView,我试图将它们设置为像书一样的页面动画。我用了一段时间的内置动画(UIViewAnimationTransitionFlipFromLeft/Right),并决定我可以做得更好。我在这里找到了一个很好的动画例子:http://fiftysixtysoftware.com/blog/2009/uiview-pageopen-sample-project/CABasicAnimation CALayer转换的可见性(重叠)问题

我能够从那里推断出我的动画效果......但是我遇到的问题是我的动画图像(正确的图像)总是在下面显示左边的图像。

这里是在初始化代码:

// set up some rects for use later 
landscapeLeftRect = CGRectMake(0, 12, 512, 725); 
landscapeRightRect = CGRectMake(512, 12, 512, 725); 

// all our images 
imageLeft = [[UIImageView alloc] initWithFrame:portraitRect]; 
imageRight = [[UIImageView alloc] initWithFrame:landscapeRightRect]; 

...和执行实际的动画(注意,这是动画的下半年,一半的“重叠”我imageLeft视图):

// set the image to be nextLeft 
//[imageRight setImage:image.nextLeft]; 
[self.view sendSubviewToBack:imageLeft]; 
[self.view bringSubviewToFront:imageRight]; 
//[imageRight.layer setZPosition:1.0f]; 
[imageRight setFrame:landscapeLeftRect]; 

// remove any previous animations 
[imageRight.layer removeAllAnimations]; 

// set up the anchorPoint and center 
if (imageRight.layer.anchorPoint.x != 1.0f) { 
    imageRight.layer.anchorPoint = CGPointMake(1.0f, 0.5f); 
    imageRight.center = CGPointMake(imageRight.center.x + imageRight.bounds.size.width/2.0f, imageRight.center.y); 
} 

// create an animation to hold the first half of the page turning forward 
CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
transformAnimation.removedOnCompletion = YES; 
transformAnimation.duration = 0.40f; 
transformAnimation.delegate = self; 
transformAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
// start the animation from the current state 
CATransform3D endTransform = CATransform3DMakeRotation(3.141f/2.0f, 
                 0.0f, 
                 -1.0f, 
                 0.0f); 
// these values control the 3D projection outlook 
endTransform.m34 = -0.001f; 
endTransform.m14 = 0.0015f; 
transformAnimation.fromValue = [NSValue valueWithCATransform3D:endTransform]; 
// we should end up at the beginning... 
transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 

[imageRight.layer addAnimation:transformAnimation forKey:nil]; 

事情我已经试过:(你可以看到无论是在上面的代码中)

  • bringSubviewToFront/sendSubviewToBac ķ
  • 提前任何建议或智慧的层上设置Z指数

感谢。同样,问题是我的imageLeft总是显示在动画之上。

+0

任何人现在看这个问题(超过两年后)应该看看页面视图控制器('UIPageViewController')和它的动画。 – livingtech

回答

2

我面临同样的问题,只是发现你可以通过设置视图的zPosition来解决它。 我用过这样的东西:

viewThatShouldBeSentToTheBack.layer.zPosition = -10000;

0

我发现,一旦我把动画视图放到前面,一切都奏效了。我正在制作翻页动画,并且在向后翻页时以逆时针方式显示页面叠加时遇到问题。我的麻烦是我看不到动画,因为它在它应该覆盖的页面后面。

+0

我的问题的关键是bringSubviewToFront对我没有任何作用。 – livingtech

0

是否确实在将页面视图添加到超级视图时正确地遍历数组?当然,你最好先把最后一页放在最前面,这样你的第一页就可以在上面了...我不会侮辱你的智力 - 图层可能会非常棘手。

+0

我在任何时候都没有遍历子视图。这里有一个有趣的二分法,我使用的动画只能应用于图层,但是否则我只能处理已添加到父图层的视图。 – livingtech

0

我遇到了这个确切的问题,我发现唯一的方法来避免它是通过设置tyhe背景的zPosition为一个很大的负数。这将是很高兴知道为什么...