有没有办法从UIView中捕获WillRotateToInterfaceOrientation事件? [英] Is there a way to catch an WillRotateToInterfaceOrientation event from an UIView?

查看:237
本文介绍了有没有办法从UIView中捕获WillRotateToInterfaceOrientation事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

every UIViewController has a method called willRotateToInterface.

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

Is it possible to do this within a UIView too?

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

Does this match the idea of model view controller ?

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

The only way I can think of is to send the event from the UIViewController to the UIView.

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

Is there a global variable for the current orientation?

推荐答案

观察 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 为你完成所有这些,所以如果你有一个视图控制器,值得让它完成工作。

I should note that yo uneed to add -beginGeneratingDeviceOrientationNotifications when you want these notifications to be sent, and call -endGeneratingDeviceOrientationNotifications when you want them to stop. There is a battery impact of generating these, so you should only do so when your view is on screen. UIViewController does all this for you, so if you have a view controller, it is worth letting it do the work.

这篇关于有没有办法从UIView中捕获WillRotateToInterfaceOrientation事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆