1
votes

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!

2
What is the angle you provided?Krishnan
It's an arbitrary angle, such as 45.0f * M_PI/180.0CodeBrew
It does work for me with any arbitrary angle...Krishnan
Just want to make sure you used myView.layer.transform instead of myView.layer.sublayerTransform, and you didn't use animation?CodeBrew
I figured out the problem. It is because my view's layer has a previous animation which has a toValue equal to identity transform and removedOnCompletion equal to NO, thus preventing subsequent non-animated transform from working. Thanks again, your test confirmed to me that the transform on layer itself does work so I could concentrate on checking other parts of code.CodeBrew

2 Answers

0
votes

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

0
votes

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.