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

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

问题描述

我一直在使用这种方法,但从来没有真正理解他们的行为。



每次我尝试使用它们,在黑暗中,我尝试不同的方面,它似乎没有做我期望它。



为了争论,我试图转换帧I

 

$ c> UIKeyboardFrameEndUserInfoKey = NSRect:{{406,0},{362,1024}}

我想将它转换为正确的矩形(交换x / y,宽度和高度),所以我做

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

但是rect会变成:

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

任何帮助将不胜感激:)

解决方案

您使用错误。它基本上做一个坐标转换。我假设它开始通过将矩形从起始视图转换为窗口坐标,然后从窗口坐标转换回第二个视图的坐标系统。这是一个例子。



您的窗口中有两个子视图,一个是{{25,0},{100,100}},框{{50,50},{200,200}}您要将以下rect {{50,50},{50,50}}从第一个子视图翻译到第二个子视图。


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


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



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.

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

In landscape mode, I get this NSRect:

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

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

But the rect becomes:

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.

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. 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}}

  2. 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 *)视图工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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