How does - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view work(- (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.
将矩形从第一个子视图的坐标转换为窗口坐标:由于我们开始的子视图的原点为 {25,0},我们需要将其转换为窗口坐标.这可以通过将子视图的原点添加到矩形的原点来完成,因此窗口坐标中的新矩形是{{75, 50}, {50, 50}}
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 如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01