ConvertPoint:fromView:在模拟器上运行良好,而不是在设备上

发布于 2024-11-07 14:22:48 字数 395 浏览 4 评论 0原文

我很高兴看到这个:有关如何转换的答案子视图的点 它在模拟器上对我来说非常有效。

但由于某种原因,它在设备上不起作用...... 难道设备对视图位置进行了不同的管理——或者这只是另一个谜团?或者(希望)我写错了,你们可以帮忙...... :)

这是我的台词:

CGPoint yoel = [imagePressed.imageView convertPoint:imagePressed.frame.origin toView:nil];

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绿萝 2024-11-14 14:22:48

我有一个与这个问题相关的不同问题,并且注意到在转换矩形时我必须考虑屏幕的比例。 (即它在模拟器上工作不是因为它是模拟器,而是因为模拟器处于非视网膜模式)

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

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));
无力看清 2024-11-14 14:22:48

好的。这是 %d 和 %f 之间差异的一个教训:)
显然它工作完美。

我的错误是 - 我在模拟器上
DLog(@"yoel 是 %f", yoel.x);
DLog(@"yoel 是 %f", yoel.y);

就在设备上运行它之前,我将其更改为
DLog(@"yoel 是 %d", yoel.x);
DLog(@"yoel 是 %d", yoel.y);

由于 CGPoint 是浮动的,所以我得到了 0 而不是正确的坐标...

另一个教训 - 在将测试从模拟器切换到设备之前我永远不会更改代码,所以不要责怪苹果,而是责怪我自己:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文