如何动画UIImageViews,如舱门打开

wes*_*wes 14 uiimageview uiviewanimation ios

我正在尝试制作一个动画,看起来像是朝向用户开放的2个法式门(或2个舱门).

我尝试使用内置的UIViewAnimationOptionTransitionFlipFromRight转换,但转换的原点似乎是UIImageView的中心而不是左边缘.基本上我有2个UIImageViews,每个填充都有屏幕.我希望动画看起来像UIImageViews从屏幕中心提升到边缘.

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

以前有人做过这样的事吗?任何帮助都是极好的!

更新:感谢Nick Lockwood的工作代码

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;
}];
Run Code Online (Sandbox Code Playgroud)

Nic*_*ood 26

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

每个视图都有一个layer属性具有可动画的子属性.在动画功能方面,您可以找到所有非常酷的东西(我建议您阅读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
Run Code Online (Sandbox Code Playgroud)

现在应用以下动画

[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"
}];
Run Code Online (Sandbox Code Playgroud)

这应该做到这一点.

免责声明:我实际上没有对此进行过测试,因此角度可能会向后倾斜,视角可能会变得棘手等等,但它至少应该是一个好的开始.

更新:好奇心让我变得更好.这是完全正常工作的代码(这假设左右门在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, 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;
    }];
}
Run Code Online (Sandbox Code Playgroud)

  • 实际上,我应该说"没有人能被告知黑客帝国是什么......" (4认同)