开发者

convertPoint:fromView: works well on the simulator, not on device

开发者 https://www.devze.com 2023-03-06 08:11 出处:网络
I was happy to see this: answer for how to convert point of a subview and it worke开发者_StackOverflowd perfect for me on the simulator.

I was happy to see this: answer for how to convert point of a subview and it worke开发者_StackOverflowd 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];


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));


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消