检测UIView子类内的自动旋转

Mat*_*era 5 iphone subclass uiview autorotate ios

我正在创建一个UIView的子类,当方向改变时需要重新布局其子视图.

它需要能够"插入任何地方"而无需任何额外的代码,所以我宁愿不依赖UIViewController方法来检测自转

无论如何,UIView知道方向何时发生了变化?

Din*_*aja 9

这样的通知如何检测方向变化?

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; 
Run Code Online (Sandbox Code Playgroud)

但是,在此之前,您需要注册以生成这样的通知.

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
Run Code Online (Sandbox Code Playgroud)