我正在尝试创建一个动画,看起来像向用户开放的2个法式门(或2个舱口门).
我尝试使用内置的UIViewAnimationoptionTransitionFlipFromright转换,但转换的起源似乎是UIImageView的中心,而不是左边缘.基本上我有两个UIImageViews,每个填充屏幕.我希望动画看起来像UIImageViews从屏幕中心提升到边缘.
[UIView transitionWithView:leftView
duration:1.0
options:UIViewAnimationoptionTransitionFlipFromright
animations:^ { leftView.alpha = 0; }
completion:^(BOOL finished) {
[leftView removeFromSuperview];
}];
有没有人做过这样的事情?任何帮助都是极好的!
更新:
工作代码感谢Nick Lockwood
leftView.layer.anchorPoint = CGPointMake(0,0.5); // hinge around the left edge
leftView.frame = CGRectMake(0,160,460); //reset view position
rightView.layer.anchorPoint = CGPointMake(1.0,0.5); //hinge around the right edge
rightView.frame = CGRectMake(160,460); //reset view position
[UIView animateWithDuration:0.75 animations:^{
CATransform3D leftTransform = CATransform3DIdentity;
leftTransform.m34 = -1.0f/500; //dark magic to set the 3D perspective
leftTransform = CATransform3DRotate(leftTransform,-M_PI_2,1,0);
leftView.layer.transform = leftTransform;
CATransform3D rightTransform = CATransform3DIdentity;
rightTransform.m34 = -1.0f/500; //dark magic to set the 3D perspective
rightTransform = CATransform3DRotate(rightTransform,M_PI_2,0);
rightView.layer.transform = rightTransform;
}];
解决方法
首先将QuartzCore库添加到您的项目,然后将#import< QuartzCore / QuartzCore.h>
每个视图都有一个层属性,子属性是可动画的.在这里,您可以找到所有非常酷的东西,当涉及到动画功能(我建议您阅读可以设置的CALayer类属性 – 它会吹动你的头脑 – 任何视图上的动态柔和的阴影?)
无论如何,回到主题.要旋转你的门打开3D,首先定位他们,如果他们被关闭,所以每个门填充一半的屏幕.
现在设置它们的view.layer.anchorPoint属性如下
leftDoorView.layer.anchorPoint = CGPoint(0,0.5); // hinge around the left edge rightDoorView.layer.anchorPoint = CGPoint(1.0,0.5); // hinge around the right edge
现在应用以下动画
[UIView animateWithDuration:0.5 animations:^{
CATransform3D leftTransform = CATransform3DIdentity;
leftTransform.m34 = -1.0f/500; //dark magic to set the 3D perspective
leftTransform = CATransform3DRotate(leftTransform,0); //rotate 90 degrees about the Y axis
leftDoorView.layer.transform = leftTransform;
//do the same thing but mirrored for the right door,that probably just means using -M_PI_2 for the angle. If you don't kNow what PI is,Google "radians"
}];
那应该这样做.
免责声明:我没有真正测试过这个,所以角度可能会倒退,而且透视角度可能是螺旋式的,但至少应该是一个好的开始.
更新:好奇心越来越好.这里是完整的工作代码(这假设左右门被布置在nib文件中的关闭位置):
- (void)viewDidLoad
{
[super viewDidLoad];
leftDoorView.layer.anchorPoint = CGPointMake(0,0.5); // hinge around the left edge
leftDoorView.center = CGPointMake(0.0,self.view.bounds.size.height/2.0); //compensate for anchor offset
rightDoorView.layer.anchorPoint = CGPointMake(1.0,0.5); // hinge around the right edge
rightDoorView.center = CGPointMake(self.view.bounds.size.width,self.view.bounds.size.height/2.0); //compensate for anchor offset
}
- (IBAction)open
{
CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1.0f/500;
leftDoorView.layer.transform = transform;
rightDoorView.layer.transform = transform;
[UIView animateWithDuration:0.5 animations:^{
leftDoorView.layer.transform = CATransform3DRotate(transform,0);
rightDoorView.layer.transform = CATransform3DRotate(transform,0);
}];
}
- (IBAction)close
{
[UIView animateWithDuration:0.5 animations:^{
CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1.0f/500;
leftDoorView.layer.transform = transform;
rightDoorView.layer.transform = transform;
}];
}