如何使像打开门的UIImageViewanimation

我试图创build一个看起来像2个法式门(或2个舱门)的animation,向用户开放。

我尝试使用内置的UIViewAnimationOptionTransitionFlipFromRight过渡,但过渡的起源似乎是UIImageView的中心,而不是左边缘。 基本上我有2个UIImageViews,每个填充有屏幕。 我希望animation看起来像UIImageView从屏幕中心到边缘。

[UIView transitionWithView:leftView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromRight animations:^ { leftView.alpha = 0; } completion:^(BOOL finished) { [leftView removeFromSuperview]; }]; 

有没有人做过这样的事情? 任何帮助都是极好的!

更新:工作代码感谢尼克洛克伍德

 leftView.layer.anchorPoint = CGPointMake(0, 0.5); // hinge around the left edge leftView.frame = CGRectMake(0, 0, 160, 460); //reset view position rightView.layer.anchorPoint = CGPointMake(1.0, 0.5); //hinge around the right edge rightView.frame = CGRectMake(160, 0, 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, 0, 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, 1, 0); rightView.layer.transform = rightTransform; }]; 

首先将QuartzCore库添加到您的项目和#import <QuartzCore/QuartzCore.h>

每个视图都具有可animation的子属性的layer属性。 在这里,你会发现所有非常酷的东西,当涉及到animationfunction(我build议阅读你可以设置CALayer类属性 – 它会打动你的思想 – 任何视图上的dynamic软阴影?)

无论如何,回到主题。 要以三维方式旋转门,首先将它们定位,就好像它们被closures一样,因此每个门都填满了一半的屏幕。

现在设置他们的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 

现在应用以下animation

 [UIView animateWithDuration:0.5 animations:^{ CATransform3D leftTransform = CATransform3DIdentity; leftTransform.m34 = -1.0f/500; //dark magic to set the 3D perspective leftTransform = CATransform3DRotate(leftTransform, M_PI_2, 0, 1, 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" }]; 

这应该做到这一点。

免责声明:我没有真正testing过这个,所以angular度可能会倒退,而且angular度可能会有些扭曲,但至less应该是一个好的开始。

更新:好奇心越来越好。 这里是完整的工作代码(这里假定左边和右边的门在nib文件的closures位置):

 - (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, M_PI_2, 0, 1, 0); rightDoorView.layer.transform = CATransform3DRotate(transform, -M_PI_2, 0, 1, 0); }]; } - (IBAction)close { [UIView animateWithDuration:0.5 animations:^{ CATransform3D transform = CATransform3DIdentity; transform.m34 = -1.0f/500; leftDoorView.layer.transform = transform; rightDoorView.layer.transform = transform; }]; }