如果你想将UIView从一个视图移动到另一个视图(例如,通过segue“转移”UIView),这可以完美地运行......
-(void)takeover:(UIView*)peel
{
// transfer "ownership" of a view from wherever it is now, to self.
// keep the position correct.
CGPoint centerInWC = [peel.superview convertPoint:peel.center toView:nil];
[self addSubview:peel];
CGPoint centerInMyViewCoords = [self convertPoint:centerInWC fromView:nil];
peel.center = centerInMyViewCoords;
}
(请参阅toView的文档:and fromView:... "If view is nil, this method instead converts to window base coordinates."
)
这是输出..
2014-07-07 19:59:04.273 before .... actual position ... {87, 107}
2014-07-07 19:59:04.278 centerInWC is ... {87, 107}
2014-07-07 19:59:04.278 centerInMyViewCoords is ... {87, 107}
没问题。一切都很棒。
如何使用主窗口,所以keyWindow ??
现在,您认为以下内容也有效:
-(void)takeover:(UIView*)peel
{
//let's try using "main window" coords...
UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
CGPoint cWC = [mainWindow convertPoint:peel.center fromView:peel.superview];
[self addSubview:peel];
CGPoint c = [self convertPoint:cWC fromView:mainWindow];
peel.center = c;
}
实际上它不起作用!
每一次,您都会获得一半实际正确值的值。
这是输出......
2014-07-07 19:59:04.273 before .... actual position ... {87, 107}
2014-07-07 19:59:04.278 cWC is ... {43.5, 53.5}
2014-07-07 19:59:04.278 c is ... {43.5, 53.5}
有人知道解释吗?干杯...