- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 [英] How does - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view work

查看:29
本文介绍了- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在断断续续地使用这种方法,但从未真正理解它们的行为方式.

I've been using that kind of methods on and off but never really understood how they behaved.

每次我尝试使用它们时,我都会发现自己在尝试不同方面时摸不着头脑,但它似乎从来没有达到我的预期.

Every time I try to use them, I find myself poking in the dark as I try different aspect and it never seem to do what I expect it to.

为了争论,我正在尝试转换从 UIKeyboard 的通知中获得的帧.

For the sake of argument, I'm trying to convert the frame I get from UIKeyboard's notification.

在横向模式下,我得到了这个 NSRect:

In landscape mode, I get this NSRect:

UIKeyboardFrameEndUserInfoKey = NSRect: {{406, 0}, {362, 1024}}

现在我想将其转换为正确的矩形(交换 x/y、宽度和高度)所以我这样做了

Now I want to convert it to the proper rect (swap x/y, width and height) so I do

CGRect compatibleRect = [self convertRect:[[[notif userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:nil];

但是矩形变成:

compatibleRect = CGRect: {{406,406}, {362, 1024}}

任何帮助将不胜感激:)

Any help would be appreciated :)

推荐答案

你用错了.它基本上进行坐标转换.我假设它首先将矩形从起始视图转换为窗口坐标,然后从窗口坐标转换回第二个视图的坐标系.这是一个例子.

You are using it wrong. It basically does a coordinate conversion. I'm assuming it starts by converting the rect from the starting view to window coordinates and then converting back from window coordinates to the coordinate system of the second view. Here's an example.

您的窗口中有两个子视图,一个带有 {{25,0}, {100, 100}} 的框架,另一个带有 {{50, 50}, {200, 200}} 的框架要将以下矩形 {{50, 50}, {50, 50}} 从第一个子视图翻译到第二个子视图.以下是它的工作原理.

You have two subviews in your window, one with a frame of {{25,0}, {100, 100}}, The other with a frame of {{50, 50}, {200, 200}} You want to translate the following rect {{50, 50}, {50, 50}} from the first subview to the second subview. Here's how it might work.

  1. 将矩形从第一个子视图的坐标转换为窗口坐标:由于我们开始的子视图的原点为 {25,0},我们需要将其转换为窗口坐标.这可以通过将子视图的原点添加到矩形的原点来完成,因此窗口坐标中的新矩形是{{75, 50}, {50, 50}}

  1. convert the rect from the first subview's coordinated to window coordinates: Since the subview we are starting from has an origin of {25,0} we need to translate it to window coordinates. This can be done by adding the origin of the subview to the origin of the rect, so the new rect in window coordinates is {{75, 50}, {50, 50}}

将新的矩形从窗口坐标转换为第二个子视图的坐标:我们从矩形{{75, 50}, {50, 50}}开始,第二个子视图的框架是{{50, 50}, {200, 200}}.现在我们将第二个子视图的原点减去矩形来进行翻译.所以我们最终得到了 {{25, 0}, {50, 50}}

convert the new rect from window coordinates to the second subview's coordinates: We start with the rect {{75, 50}, {50, 50}}, The frame of the second subview is {{50, 50}, {200, 200}}. Now we subtract the origin of the second subview to the rect to do our translation. So we end up with a translated rect of {{25, 0}, {50, 50}}

这篇关于- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆