2013-06-25 36 views
2

我创建了具有前视图&背视图的访问卡。当我点击前视图时,它应该翻转UIView &显示视图的背面。使用CATransform3D翻转两个UIView

这里是我试图代码:

CATransform3D transform = CATransform3DMakeRotation(M_PI, 0, 1, 0); 
transform.m34 = 1.0/700.0; 


CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
rotation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 
rotation.toValue = [NSValue valueWithCATransform3D:transform]; 
rotation.duration = DURATION; 

CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:@"position"]; 
translation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.imageview.center.x,[[self.imageview superview] center].y-45)]; 
translation.toValue = [NSValue valueWithCGPoint:CGPointMake(_frame.origin.x+_frame.size.width/2, 
                  _frame.origin.y+_frame.size.height/2)]; 
translation.duration = DURATION; 


CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:@"position"]; 
translation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.imageview.center.x,[[self.imageview superview] center].y-45)]; 
translation.toValue = [NSValue valueWithCGPoint:CGPointMake(_frame.origin.x+_frame.size.width/2, 
                  _frame.origin.y+_frame.size.height/2)]; 
translation.duration = DURATION; 
CAAnimationGroup *group = [CAAnimationGroup animation]; 

group.animations = @[ translation, rotation ]; 
group.duration = DURATION; 
group.delegate = self; 
group.fillMode = kCAFillModeForwards; 
group.removedOnCompletion = NO; 
[layer addAnimation:group forKey:nil]; 

上面的代码完全适用于单一视图。如何结合第二视图从前面&背面效果翻转。

enter image description here

+1

关于'_frame.origin.x + _frame.size.width/2':为什么不使用'CGRectGetMidX(_frame)'? –

回答

3

使用的Core Animation

您可以添加正面和背面的容器视图。后视图将围绕Y轴旋转180度,前部将正常。两个层将是单面(通过设置layer.doubleSided = NO;

然后,当在应用旋转你将动画容器视图的旋转,使得正面和背面动画同时。

UIView的过渡

或者你可以只使用内置的翻转动画

transitionFromView:toView:duration:options:completion: 

,并通过其中UIViewAnimationOptionTransitionFlipFromLeftUIViewAnimationOptionTransitionFlipFromRight的选项。

+1

Ronnqvist谢谢你给我提示。 layer.doublesided方法帮了我很多。 – Anand