Iphone 在UIView子类中检测自动旋转
我正在创建UIView的一个子类,它需要在方向改变时重新显示其子视图 它需要能够“插入任何地方”,而不需要任何额外的代码,因此我不希望依赖UIViewController方法来检测自动旋转Iphone 在UIView子类中检测自动旋转,iphone,ios,uiview,subclass,autorotate,Iphone,Ios,Uiview,Subclass,Autorotate,我正在创建UIView的一个子类,它需要在方向改变时重新显示其子视图 它需要能够“插入任何地方”,而不需要任何额外的代码,因此我不希望依赖UIViewController方法来检测自动旋转 UIView是否可以知道方向何时发生了变化?像这样的通知如何检测方向变化 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientatio
UIView是否可以知道方向何时发生了变化?像这样的通知如何检测方向变化
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
但是,在此之前,您需要注册以生成这样的通知
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
我发现这比UIDeviceOrientationIDChangeNotification更有效:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("layoutControls"), name: UIApplicationDidChangeStatusBarOrientationNotification, object: nil)
我甚至不需要在BegingeratingDeviceOrientationNotifications中添加第一行。在Swift中,将按如下方式完成:
NotificationCenter.default.addObserver(self, selector: #selector(orientationChange(inNotification:)), name: NSNotification.Name.UIApplicationDidChangeStatusBarOrientation, object: nil)
@objc private func orientationChange(inNotification:Notification) -> Void {
// handle notification
}