我想從我的UIView轉換一個CGPoint到UIWindow座標,並意識到UIApplication keyWindow總是零;爲什麼是這樣?UIApplication sharedApplication - keyWindow是否爲零?
我試過了UIView的convertPoint:toView:
方法。
請參見本文的示例代碼我在視圖控制器試圖在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應該在這裏工作,但是沒有。 這是爲什麼?
如果你的應用程序有多個窗口,這仍然有效嗎?喜歡,基於文檔的應用程序呢? – 2013-06-18 18:13:03
你的意思是[UIApplication sharedApplication] .keyWindow – Laszlo 2013-12-11 14:49:31
@Laszlo這不是一回事。一個從應用程序委託中獲得窗口,一個窗口可能位於頂端。 – iwasrobbed 2017-11-08 19:32:41