旋转时视图移出位置
我创建了一个 UIViewController (基于 如何在旋转时切换视图)来切换设备旋转时在 2 个视图之间切换。每个视图都是针对特定方向“专门化”的。
它使用 UIDeviceOrientationDidChangeNotification 通知来切换视图:
-(void) deviceDidRotate: (NSNotification *) aNotification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSLog(@"Device rotated to %d!", orientation);
if ((orientation == UIDeviceOrientationPortrait) ||
(orientation == UIDeviceOrientationPortraitUpsideDown)) {
[self displayView:self.portraitViewController.view];
}else if ((orientation == UIDeviceOrientationLandscapeLeft) ||
(orientation == UIDeviceOrientationLandscapeRight)) {
[self displayView:self.landscapeViewController.view];
}
}
以及其他一些工作。当我旋转到横向然后返回纵向时,问题就会出现。当返回纵向时,子视图不会显示在正确的位置,特别是 UIPickerView:
第一次纵向:
旋转到横向:
返回肖像:
如果我重复旋转过程,事情只会变得更糟。我做错了什么?
源代码在这里: http://dl.dropbox.com/u/ 3978473/forums/Rotator.zip
提前致谢!
I created a UIViewController (based on How to switch views when rotating) to switch between 2 views when the device rotates. Each view is "specialized" for a particular orientation.
It uses the UIDeviceOrientationDidChangeNotification notification to switch views:
-(void) deviceDidRotate: (NSNotification *) aNotification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSLog(@"Device rotated to %d!", orientation);
if ((orientation == UIDeviceOrientationPortrait) ||
(orientation == UIDeviceOrientationPortraitUpsideDown)) {
[self displayView:self.portraitViewController.view];
}else if ((orientation == UIDeviceOrientationLandscapeLeft) ||
(orientation == UIDeviceOrientationLandscapeRight)) {
[self displayView:self.landscapeViewController.view];
}
}
and sort of works. The problems shows up when I rotate to Landscape and then back to Portrait. When going back to portrait the subviews aren't displayed in the right place, specially the UIPickerView:
First Time Portrait:
Rotate to Landscape:
Back to Portrait:
If I repeat the rotation process, things just get worse. What am I doing wrong?
The source code is here: http://dl.dropbox.com/u/3978473/forums/Rotator.zip
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要解决偏移问题,请重写 displayView: 方法,如下所示。
-(void)displayView:(UIView *)aView{
self.view = aView;
然而
,旋转很奇怪。你应该检查那部分代码。
使用 UIViewController 旋转方法
而不是 -(void)deviceDidRotate:
更简单,您将避免那种奇怪的弹跳,并且不再需要通知。
阅读有关我上面指定的方法的苹果文档。
希望这有帮助。
To solve your offset problems, rewrite the displayView: method as below.
-(void) displayView: (UIView *)aView{
self.view = aView;
}
Rotations however are strange. you should review that part of code.
Use the UIViewController rotation methods
instead of -(void)deviceDidRotate:
Much simpler, you will avoid that strange bouncing, and you don't need notifications any more.
Do some reading on the apple documentation on the methods i specified above.
Hope this helps.
好的,我发现了错误。这非常简单和愚蠢:我混合了框架和边界。
在 displayView: 代码中,我将子视图的框架设置为父视图的框架,而它应该是父视图的边界。
OK, I found the error. It's pretty simple and stupid: I mixed frame and bounds.
In the displayView: code I was setting the frame of the child view to the frame of the parent view, when it should be the bounds of the parent.