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

Iphone 有没有办法从UIView捕获WillRotateToInterfaceOrientation事件?,iphone,objective-c,uiview,uiviewcontroller,Iphone,Objective C,Uiview,Uiviewcontroller,每个UIViewController都有一个名为willRotateToInterface的方法 是否也可以在UIView中执行此操作 这是否符合模型-视图-控制器的概念 我能想到的唯一方法是将事件从UIViewController发送到UIView 当前方向是否有全局变量?观察UIDeviceOrientationIDChangeNotification: [[NSNotificationCenter defaultCenter] addObserver:self selector:@sele

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

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

这是否符合模型-视图-控制器的概念

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


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

观察
UIDeviceOrientationIDChangeNotification

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

...

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


我应该注意,当您希望发送这些通知时,您需要添加
-begingeratingdeviceorientationnotifications
,当您希望停止这些通知时,请调用
-endGeneratingDeviceOrientationNotifications
。生成这些视图会对电池造成影响,所以您应该只在屏幕上显示视图时才这样做
UIViewController
为您完成所有这一切,因此,如果您有一个视图控制器,让它来完成这项工作是值得的。

您可以订阅全局通知并在设备旋转时接到呼叫,但它不会为您做任何事情

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

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


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

如果在layoutSubviews调用中所做的更改是可设置动画的,这将是一个完美的解决方案。