2

I've tried this:

CATransform3D rotationTransform = [[self.layer presentationLayer] transform];

This will not work, since the compiler will throw an warning and an error:

Warning: Multiple methods "-transform" found. Error: Invalid initializer.

So then I tried this:

CATransform3D rotationTransform = [[self.layer presentationLayer] valueForKey:@"transform"];

Error: Invalid initializer.

What's wrong with that?

Thanks
  • 40,109
  • 71
  • 208
  • 322

1 Answers1

6

-presentationLayer returns an id. You need to cast it back to a CALayer:

CATransform3D rotationTransform = [(CALayer*)[self.layer presentationLayer] transform];

To be very safe, you would check first:

CATransform3D rotationTransform = {0};
CALayer* presentationLayer = (CALayer*)[self.layer presentationLayer];
if( [presentationLayer respondsToSelector:@selector(transform)]) {
    rotationTransform = presentationLayer.transform;
}

But in practice, this is going to be fine without this check.

In the second case, -valueForKey: also returns an id, which you are trying to assign to struct, which is not possible to do implicitly. You can't just cast an object pointer into a struct.

JJD
  • 50,076
  • 60
  • 203
  • 339
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • I also fixed that in the original answer which triggered this question: http://stackoverflow.com/questions/877198/is-there-a-way-to-figure-out-how-many-degrees-an-view-is-rotated-currently-durin/878618#878618 . He could have waited a little bit and I would have amended my answer, rather than asking a whole new question. However, this might be useful to keep as a standalone for people Googling something similar. – Brad Larson May 18 '09 at 18:08
  • Just to add here... I was struggling with this: Every Transform must take place from the point where it was set to in the last position. So that a view rotated to 15 degrees should be rotated from 15 degree to any other angle. i.e setting the from value is very necessary otherwise you will see a transform with glitch – nr5 Sep 05 '16 at 09:53