Question

I'm trying to apply a perspective transform to my UIView object with the following code:

CATransform3D t = CATransform3DIdentity;
t.m34 = -1.0/1000;
t = CATransform3DRotate(t, angle, 0.0f, 1.0f, 0.0f);

myView.layer.transform = t;

and I don't see any effect at all. I tried other transforms like a simple translation and they don't work either.

However, if I do either of the following two modifications then it will work somewhat but neither satisfies my request:

  1. Change the last line to

    myView.layer.sublayerTransform = t;

This sort of works but it only transforms the subviews on myView, not myView itself.

  1. Add an animation code to apply the change instead of directly assign the change to the layer:

    CABasicAnimation *turningAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; turningAnimation.toValue = [NSValue valueWithCATransform3D:t]; turningAnimation.delegate = self; turningAnimation.fillMode = kCAFillModeForwards; turningAnimation.removedOnCompletion = NO;

    [myView.layer addAnimation:turningAnimation forKey:@"turning"];

The thing is that I don't want the animation.

Can anybody point a direction for me?

Thanks!

Was it helpful?

Solution 2

I figured out the problem after test by Krishnan confirmed that layer.transform itself does work without the aid of animation or sublayerTransform. The problem I encountered was caused by an animation that had been applied to my view's layer and was not subsequently removed. Since this animation had a toValue equal to the identity transform, I didn't realize it can actually prevent subsequent non-animated transforms from working. Setting removedOnCompletion = YES (which is default) on the animation solved the mystery.

OTHER TIPS

You should be able to just use myView.transform = CGAffineTransformMakeRotation(angle). It won't animate the property unless you explicitly tell it to using [UIView animateWithDuration:]. Using the UIView transform property will ultimately apply your transformation to the CALayer that backs UIView, so I would make sure to only interact with the transforms on one level (UIView or CALayer).

UIView transform doc: https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/instp/UIView/transform

CGAffineTransform Doc:

https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGAffineTransform/Reference/reference.html#//apple_ref/c/func/CGAffineTransformMakeRotation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top