由于上手这个项目不久,接手的时候其他模块已经写的七七八八了,目前其他界面只支持横屏,而我的附件浏览却要横竖屏都支持。
不怕,咱有这几个方法就so easy啦
//ios6.0后 判断当前是否能够转屏
- (BOOL)shouldAutorotate
{
if (_curDataLookoverModel == DataLookoverModelRemark) {
//设定在批注模式下,不允许旋转页面
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}else{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
}
return YES;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortraitUpsideDown;
}
//当前进来的时候
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return [[UIApplication sharedApplication] statusBarOrientation];
}
如果
- (BOOL)shouldAutorotate
返回YES了,也就是说支持转屏,那么接下来就会执行以下两个方法
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
#ifdef DEBUGX
NSLog(@"%s %@ (%d)", __FUNCTION__, NSStringFromCGRect(self.view.bounds), toInterfaceOrientation);
#endif
if (_isVisible == NO) return; // iOS present modal bodge
}
//当屏幕出现旋转开始并在动画发生之前自动调用此方法 重新设置控件的大小与位置
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[self relayoutForInterfaceOrientation:toInterfaceOrientation withDuration:duration];//重新布局控件位置和大小
if (_isVisible == NO) return;
[self updateScrollViewContentViews]; //旋转时需要重新绘制pdf页面
_lastAppearSize = CGSizeZero;
}
但是发现有个奇怪的bug,进来这个界面的时候这些方法都会调用,第一次转屏由横屏转成竖屏 的时候也调用,但是发现转成竖屏后回不来了,怎么转,这
shouldAutorotate方法就是不调用,它不调用的话,接下来的方法也就不会调用了,所以就无法转屏了,搞了我大半天没找到原因。。。后来才发现在Appdelegate里面有这么2个方法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
if (self.allowRotation == YES) {
//横屏
return UIInterfaceOrientationMaskLandscape;
}else{
//竖屏
return UIInterfaceOrientationMaskPortrait;
}
}
// 支持设备自动旋转
- (BOOL)shouldAutorotate
{
if (_allowRotation == 1) {
return YES;
}
return NO;
}
我擦,原来这里已经设置了返回的支持的方向只有横屏UIInterfaceOrientationMaskLandscape 或者 竖屏的
UIInterfaceOrientationMaskPortrait ,这就难怪了,因为根据这个属性
self.allowRotation
来判断只能返回这两种,而我们的设备这个枚举可是有好几种的方式,所以我就把竖屏的这个改成UIInterfaceOrientationMaskAll支持所有方向的变化。
这一改不得了了,回到我浏览文件的界面,随便怎么转都会调用shouldAutorotate这个方法啦,所以下次得注意点是否有这个坑了。