1

Ok the scenario is that I have a custom class which creates a view containing an image and handles all its own touch events. Occasionally I want to check if moving it would cause it to overlap another view on a parent view. So after the relevent touch event I call a method on the parent view passing it the last touch cordinate and return a BOOL. This works fine while the application remains in portrait orientation.

Problem is after rotating the device and all the images are auto-rotated for me the CGPoint being passed is now in a rotated coordinate system compared with the parent view and the CGRectContainsPoint method being called no longer works properly.

In my willAnimateRotationToInterfaceOrientation method I am adjusting the position of each custom view to improve the layout for the new orientation e.g.

view.frame = CGRectMake((view.frame.origin.x*1.5),(view.frame.origin.y/1.5),view.frame.size.width,view.frame.size.height);

I read that calling the setBound method would then reset the coordinate system:

[view setBounds:view.frame];

but this isn't making a difference in my case. I also tried tinkering with 'convertPoint:toView' but the values it was giving weren't making much sense either. I concluded it wouldn't work in the case where the coordinate system had been rotated?

2 Answers 2

1

When your view is rotated (using the autorotate mechanism), iOS simply apply a CGAffineTransform to your view, to rotate its content.

So you should be able to retrieve the current CGAffineTransform applied to your UIView, then apply this CGAffineTransform to the point of your UITouch using CGPointApplyAffineTransform (probably after inverting the AffineTransform, to apply the inverse transformation to your point. Haven't tested this code, only writing this from memory)

UITouch* touch = [touches anyObject];
CGPoint pt = [touch locationInView:self];
CGAffineTransform inverseTransform = CGAffineTransformInvert(self.transform);
CGPoint transformedPoint = CGPointApplyAffineTransform(pt,inverseTransform);
9
  • Hi thanks for the reply. I have tried to implement this but didn't have much success. I put a break point in and looked at the values for the 'inverseTransform' but it doesn't seem to vary with the orientation of the screen? Aug 29, 2011 at 22:21
  • Just to confirm, the transform of my main UIView nor any of subviews I'm interested in seem to change when I rotate the screen (a 1.0 b 0.0 c 0.0 d 1.0). Aug 29, 2011 at 23:16
  • OK so maybe it's not the transform of the right view. Generally it's the transform of the view of your ViewController itself. If you can't find it, try investigating from the view back up to all its superviews. Aug 30, 2011 at 15:20
  • My Hierarchy is the Window, TabbedViewController, UIView, customSubViews. From the UIView I have tried checking the transform of the parent and its (a 0.0 b 0.0 c 0.0 d 0.0) and doesnt change with rotation either. I'm seemingly out of views to check. Aug 30, 2011 at 15:37
  • Unfortunately I don't have my Mac near me right now to check myself, but it seems strange that none of your transforms are different from the Identity transform... I wonder how come you view can be rotated then?! Maybe try testing the CATransform of the layers of your view instead? I'll try to do some tests on my own If I got some time before the weekend Aug 31, 2011 at 13:11
0

The view of root viewController always acts like portrait mode. You should insert a new view inside of the root one. And this new view will acts correctly, will give right size and coordinates according to Apple says.

for example ;

UIView *v = self;
while ([[v superview] superview] != NULL) {
    v = [v superview];
}

UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:v];

touchPoint will be the correct one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.