我想将 CGPoint 从我的 UIView 转换为 UIWindow 坐标,并意识到 UIApplication keyWindow 始终为零;为什么是这样?
我已经尝试过convertPoint:toView:
UIView 的方法。
请参阅我在 Xcode 模板中的视图控制器中尝试的示例代码(视图应用程序):
- (void)viewDidLoad {
[super viewDidLoad];
UIView *test = [[UIView alloc] initWithFrame:CGRectMake(40,40,250,250)];
[test setBackgroundColor:[UIColor redColor]];
[self.view addSubview:test];
CGPoint p = CGPointMake(100, 100);
CGPoint np;
np = [test convertPoint:p toView:[[UIApplication sharedApplication] keyWindow]];
NSLog(@"p:%@ np:%@", NSStringFromCGPoint(p), NSStringFromCGPoint(np));
AppDelegate *appDel = (AppDelegate *)[UIApplication sharedApplication].delegate;
np = [test convertPoint:p toView:[appDel window]];
NSLog(@"p:%@ np:%@", NSStringFromCGPoint(p), NSStringFromCGPoint(np));
np = [test convertPoint:p toView:nil];
NSLog(@"p:%@ np:%@", NSStringFromCGPoint(p), NSStringFromCGPoint(np));
[test release];
if(![[UIApplication sharedApplication] keyWindow])
NSLog(@"window was nil");
}
我得到:
p:{100, 100} np:{100, 100}
p:{100, 100} np:{140, 160}
p:{100, 100} np:{100, 100}
window was nil
我可以转换它,但只有当我通过应用程序委托访问窗口时。而不是 UIApplication。根据文档, keyWindow 应该在这里工作,但为零。为什么是这样?