Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First add the QuartzCore library to your project and <code>#import &lt;QuartzCore/QuartzCore.h&gt;</code></p> <p>Every view has a <code>layer</code> property with sub-properties that are animatable. This is where you'll find all the really cool stuff when it comes to animation capabilities (I suggest reading up on the <code>CALayer</code> class properties you can set - it will blow your mind - dynamic soft drop shadows on any view?)</p> <p>Anyway, back on topic. To rotate your doors open in 3D, first position them as if they were closed, so with each door filling half the screen.</p> <p>Now set their <code>view.layer.anchorPoint</code> properties as follows</p> <pre><code>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 </code></pre> <p>Now apply the following animation</p> <pre><code>[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" }]; </code></pre> <p>And that should do it.</p> <p>DISCLAIMER: I've not actually tested this, so the angles may be backwards, and the perspective may be screwy, etc. but it should be a good start at least.</p> <p>UPDATE: Curiosity got the better of me. Here is fully working code (this assumes that the left and right doors are laid out in the closed position in the nib file):</p> <pre><code>- (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; }]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload