做橫豎屏最重要的是確定橫豎屏響應的接口粘茄。目前我知道的有兩種方式 :
1.使用通知迹蛤。
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-
(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)_orientationDidChange:(NSNotification*)notify
{
[self _shouldRotateToOrientation:(UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation];
}
-(void)_shouldRotateToOrientation:(UIDeviceOrientation)orientation {
if (orientation == UIDeviceOrientationPortrait ||orientation == UIDeviceOrientationPortraitUpsideDown) {
// 豎屏
}else {
// 橫屏
}
}
上述代碼,一看就明白。
2.使用 viewWillLayoutSubviews
測試發(fā)現(xiàn)橫豎屏切換的時候,系統(tǒng)會響應一些函數(shù),其中 viewWillLayoutSubviews就是之一态蒂。
- (void)viewWillLayoutSubviews
{
[self _shouldRotateToOrientation:(UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation];
}
通過上述一個函數(shù)就知道橫豎屏切換的接口了。
注意:
viewWillLayoutSubviews只能用在ViewController里面费什,在view里面沒有響應钾恢。