首页 > convertRect:view.frame toView:window x 异常翻倍?

convertRect:view.frame toView:window x 异常翻倍?

    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    NSLog(@"window.f %@", NSStringFromCGRect(window.frame));
    CGRect newFrame = [view convertRect:view.frame toView:window]; 
    NSLog(@"原始:%@ 转换:%@", NSStringFromCGRect(view.frame), NSStringFromCGRect(newFrame));

window.f {{0, 0}, {320, 480}}
原始:{{85, 7}, {150, 30}} 转换:{{170, 34}, {150, 30}}

为什么 x 被翻倍了?
View 为灰色控件

如果将控件放到view 中

2016-03-07 02:39:25.662 微博2期[57203:938330] window.f {{0, 0}, {320,
480}} 2016-03-07 02:39:25.662 微博2期[57203:938330] 原始:{{20, 60}, {40,
20}} 转换:{{40, 184}, {40, 20}}

得到以上数据。。。184 - 64 正好 120,也就是说x * 2, y * 2 了

将控件添加到一个视图中,相对于视图转换(这个比较难说明白,简单说就是排除了各种bar的干扰)结果直接x y 都双倍了。。。


[view convertRect:view.frame toView:window] -> [view convertRect:view.frame toView:nil]
改成这样的就不会存在题主的描述的现象。


两种正确写法

[view.superview convertRect:view.frame  toView:nil];
[view           convertRect:view.bounds toView:window];

对convertRect 调用者理解有误,应该是需要转换控件的参考系视图,而不是自身。

【热门文章】
【热门文章】