Search code examples
objective-ciosuiviewcore-animationtransform

iOS Align a UIView parallel to X-Z plane


I realize that by default a UIView is parallel to the X-Y plane, the Z-axis coming straight at you.

How do I rotate this UIView so that it is parallel to X-Z plane? I think I have to use CATransform3D but am not sure what angle I should give it? M_PI_2 or -M_PI_2 ?

Anybody has any idea? Thanks for help...

EDIT: I know that by doing this, the UIView will not be visible to the user, but I want to set a particular UIView this way & then do some rotation animation on it. So this is what I want..


Solution

  • WHat you probably want to do is to apply a transformation to the view's backing layer. The transform property on UIView is an affine transformation, not allowing any 3D manipulation. But accessing view.layer.transform you have a full 4x4 matrix for your transformation needs, allowing nice funky 3d stuff.

    1. Add QuartzCore.framework to your target.
    2. Import <QuartzCore/QuartzCore.h> to get access to the CALayer API.
    3. Code away.

    For example:

    // Rotate 1/4 by the Y-axis.
    myView.layer.transform = CATransform3DMakeRotation(M_PI_2, 0.0f, 1.0f, 0.0f);