2013-06-25 104 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