有没有办法从UIView捕获WillRotateToInterfaceOrientation事件?

时间:2009-09-01 15:26:02

标签: iphone objective-c uiview uiviewcontroller

每个UIViewController都有一个名为willRotateToInterface的方法。

是否可以在UIView中执行此操作?

这是否符合模型视图控制器的想法?

我能想到的唯一方法是将事件从UIViewController发送到UIView。

当前方向是否有全局变量?

3 个答案:

答案 0 :(得分:17)

观察UIDeviceOrientationDidChangeNotification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

...

- (void)orientationDidChange:(NSNotification *)note
{
    NSLog(@"new orientation = %d", [[UIDevice currentDevice] orientation]);
}

UIDevice Class Reference

我应该注意,当您希望发送这些通知时,您可以添加-beginGeneratingDeviceOrientationNotifications,并在希望它们停止时调用-endGeneratingDeviceOrientationNotifications。生成这些产品会对电池产生影响,因此只有当您的视图在屏幕上时才会这样做。 UIViewController为你完成所有这些,所以如果你有一个视图控制器,那么值得让它完成这项工作。

答案 1 :(得分:3)

如果您只想在方向更改时将视图调整为新尺寸/布局,则应该覆盖其layoutSubviews方法。

只要视图大小发生变化,就会调用它,这通常在视图旋转时发生。

答案 2 :(得分:2)

您可以订阅全局通知并在设备轮换时接听电话,但它不会为您做任何事情。

[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
相关问题