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

jan*_*mon 6 iphone objective-c uiviewcontroller uiview

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

是否有可能在UIView中做到这一点?

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

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

当前方向是否存在全局变量?

Rob*_*ier 17

观察UIDeviceOrientationDidChangeNotification:

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

...

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

UIDevice类参考

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