【发布时间】:2010-11-24 16:29:27
【问题描述】:
每个 UIViewController 都有一个名为 willRotateToInterface 的方法。
是否也可以在 UIView 中执行此操作?
这是否符合模型视图控制器的想法?
我能想到的唯一方法是将事件从 UIViewController 发送到 UIView。
当前方向是否有全局变量?
【问题讨论】:
标签: iphone objective-c uiview uiviewcontroller
每个 UIViewController 都有一个名为 willRotateToInterface 的方法。
是否也可以在 UIView 中执行此操作?
这是否符合模型视图控制器的想法?
我能想到的唯一方法是将事件从 UIViewController 发送到 UIView。
当前方向是否有全局变量?
【问题讨论】:
标签: iphone objective-c uiview uiviewcontroller
关注UIDeviceOrientationDidChangeNotification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
...
- (void)orientationDidChange:(NSNotification *)note
{
NSLog(@"new orientation = %d", [[UIDevice currentDevice] orientation]);
}
我应该注意,当您希望发送这些通知时,您不需要添加-beginGeneratingDeviceOrientationNotifications,而当您希望它们停止时,您需要调用-endGeneratingDeviceOrientationNotifications。生成这些会影响电池,因此您应该仅在您的视图在屏幕上时这样做。 UIViewController 为您完成所有这些工作,所以如果您有一个视图控制器,值得让它来完成这项工作。
【讨论】:
您可以订阅全局通知并在设备旋转时接听电话,但它不会为您做任何事情..
[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
【讨论】:
如果您只想在方向更改时将视图调整为新的大小/布局,您应该重写其layoutSubviews 方法。
每当视图大小发生变化时都会调用它,这通常发生在视图旋转时。
【讨论】: