Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在UIView子类中检测自动旋转_Iphone_Ios_Uiview_Subclass_Autorotate - Fatal编程技术网

Iphone 在UIView子类中检测自动旋转

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的一个子类,它需要在方向改变时重新显示其子视图

它需要能够“插入任何地方”,而不需要任何额外的代码,因此我不希望依赖UIViewController方法来检测自动旋转


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
 }