2

我创建了具有前视图和后视图的名片。当我点击前视图时,它应该翻转 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];

上面的代码非常适合单视图。如何结合第二个视图来获得前后翻转效果。

在此处输入图像描述

4

1 回答 1

3

使用核心动画

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

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

UIView 过渡

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

transitionFromView:toView:duration:options:completion:

并通过UIViewAnimationOptionTransitionFlipFromLeftUIViewAnimationOptionTransitionFlipFromRight通过选项。

于 2013-06-25T11:57:22.670 回答