Search code examples
iphonecore-animation

convertRect fromView not returning correct frame after rotation


I have a problem I've run into with the UIView method convertRect: fromView: method. Here is the situation: I have an overwritten the UIView class to create a view that rotates with the user's movement(very similar to the TaskRabbit spinner). To create the rotation, over I added an additional view to my subclassed view, and I rotated that view. The rotated view contains additional subviews that obviously rotate with the rotated subview. The problem is, after the subview has been rotated, I need to find where those additional subviews are, with respect to the original overritten view - not the rotated view. To do this, in my UIView class, I have the following:

[self convertRect:currentView.frame fromView:rotationView];

However, when I print out the frame of the converted rect, the coordinates are not accurate. Has anyone run into this issue where the convertRect: fromView: isn't accurate after the view is rotated?

Edit

Specifically, about the points being not accurate, I can't even see the relationship between what is should be and what it is-ie off by an specific angle, x/y flipped etc. For example, the point that should be (25, 195) is returned at (325.25, 273.16)


Solution

  • I'm assuming that you are rotating your views by applying a transform to them (either a CGAffineTransform to the view or a CATransform3D to the layer). This is what is causing the problem with your frame. The documentation for UIView frame says:

    Warning If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.

    As you've already seen, the value of the frame is undefined. You can still use the center and bounds properties though.