Question

I was happy to see this: answer for how to convert point of a subview and it worked perfect for me on the simulator.

But for some reason it doesn't work on the device... Can it be that the views positions are differently managed by the device - or is it just another mystery? Or (hopefully) I wrote it wrongly and you guys can help... :)

here's my line:

CGPoint yoel = [imagePressed.imageView convertPoint:imagePressed.frame.origin toView:nil];
Was it helpful?

Solution 2

OK. that was a lesson in the difference between %d and %f :) apparently it works perfect.

My mistake was - I had on the simulator DLog(@"yoel is %f", yoel.x); DLog(@"yoel is %f", yoel.y); just before running it on the device I changed it to DLog(@"yoel is %d", yoel.x); DLog(@"yoel is %d", yoel.y); since CGPoint is in float, I got 0 instead of the right coordinate...

Another lesson learnt - I shall never change code before I switch test from simulator to device, so not to blame apple but myself :)

OTHER TIPS

I had a different problem that relates to this question, and have noticed that I had to consider the scale of the screen when converting the rect. (i.e. it worked on the simulator not because it's the simulator, but because the simulator was in non-retina mode)

// the following convertRect is like asking a view: "what would one of your subviews (aView) have for a frame in my (toView) coordinate space if it were to become my subview?
    _originalFrame = [[aView superview] convertRect: aView.frame toView: self];

    CGFloat scale = [[UIScreen mainScreen] scale];

    _originalFrame.origin.x /= scale;
    _originalFrame.origin.y /= scale;
    _originalFrame.size.width /= scale;
    _originalFrame.size.height /= scale;

    NSLog(@"Converted Frame: %@", NSStringFromCGRect(_originalFrame));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top