使用CATransform3D翻转两个UIView

时间:2013-06-25 11:38:54

标签: iphone objective-c core-animation uiviewanimation

我创建了具有前视图和前视图的访问卡。后视图。当我点击前视图时,它应该翻转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 个答案:

答案 0 :(得分:3)

使用核心动画

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

然后,当您应用旋转时,您将为容器视图的旋转设置动画,以便前后两者同时显示动画。

UIView过渡

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

transitionFromView:toView:duration:options:completion:

并传递UIViewAnimationOptionTransitionFlipFromLeftUIViewAnimationOptionTransitionFlipFromRight作为选项。