检测UIView子类内的自动旋转
问题描述:
我创建了UIView的子类,当方向更改时需要重新布局其子视图。检测UIView子类内的自动旋转
它需要能够“插在任何地方”,没有任何额外的代码,所以我宁愿不依赖于UIViewController的方法来检测自转
反正对UIView的,知道什么时候该方向有改变了吗?
答
这样的通知如何检测方向变化?
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
但是,在此之前,您需要注册以生成类似这样的通知。
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
答
我发现,我这工作比UIDeviceOrientationDidChangeNotification更好:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("layoutControls"), name: UIApplicationDidChangeStatusBarOrientationNotification, object: nil)
我甚至没有需要与beginGeneratingDeviceOrientationNotifications添加的第一行。