我的对象上有两个 UIImageViews,我正在尝试将它们设置为像书一样的页面。我使用了一些内置动画一段时间(UIViewAnimationTransitionFlipFromLeft/Right
),并决定我可以做得更好。我在这里找到了我想要的一半动画的一个很好的例子:http: //fiftysixtysoftware.com/blog/2009/uiview-pageopen-sample-project/
我能够从那里推断出让我的动画工作......但我遇到的问题是我的动画图像(右图像)总是显示在左图像下方。
这是 init 中的代码:
// 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];
我尝试过的事情:(你可以在上面的代码中看到)
- 将SubviewToFront/sendSubviewToBack
- 在图层上设置 Z 索引
提前感谢您的任何建议或智慧。同样,问题在于我的 imageLeft 总是显示在动画的顶部。