iOS开发中屏幕旋转(一)

Morris_ 2018.11.24

前言

最近做一个关于在线视频互动的iPad项目,部分界面只要横屏,部分界面可以横竖屏转换。看了看别家做的项目,有些是只做竖/横屏,有些是支持了横竖屏转换,在iPad上做好横竖屏支持给用户的体验还是挺不错的,有很多应用对横竖屏的转换做的很好,用起来很舒服,很合理,和用iPhone的体验还是挺不一样的。

一、基础知识

1、UIDeviceOrientation & UIInterfaceOrientation

The physical orientation of the device.

设备的方向,即手机或者iPad的方向,可以理解为设备是直立、倒立还是平躺,扣着放等。

//设备方向未知
UIDeviceOrientationUnknown,
//设备直立,顶部朝上,即home键在下,
UIDeviceOrientationPortrait,
//设备倒立,顶部朝下,即home键在上
UIDeviceOrientationPortraitUpsideDown, 
//设备横立,顶部在左,home键在左
UIDeviceOrientationLandscapeLeft,
//设备横立,顶部在又,即home键在又
UIDeviceOrientationLandscapeRight,
//设备面朝上平方在桌面上
UIDeviceOrientationFaceUp,
//设备面朝下,扣在桌面上
UIDeviceOrientationFaceDown

The orientation of the app's user interface.

App的界面方向,即应用里的某个界面展示方向。

//界面方向未知
UIInterfaceOrientationUnknown,
//界面是竖直展示的,一般手机App界面的方向都是竖直的
UIInterfaceOrientationPortrait,
//界面是竖直展示的,只是上下颠倒了一下
UIInterfaceOrientationPortraitUpsideDown,
//界面是横着显示的,home键在左
UIDeviceOrientationLandscapeRight,
//界面是横着显示的,home键在又
UIDeviceOrientationLandscapeLeft

UIDevice的orientation是设备顶部的方向,所以UIDeviceOrientationLandscapeLeft指的是设备顶部在左侧,即home键在又。

两者的区别和联系:UIDeviceOrientation是设备的方向,UIInterfaceOrientation是应用程序操作界面的方向,设备方向是不与App的界面操作方向有必然的绑定关系的。设备的方向变化可以导致应用界面方向变化,这要看在应用中是如何设置了。


2、UIDeviceOrientationDidChangeNotification & UIApplicationDidChangeStatusBarOrientationNotification

第一个是设备方向改变的监听,第二个是状态栏方向改变的监听。设备方向改变可以造成状态栏方向变化,但状态栏方向变了,设备方向不一定改变。

//屏幕旋转监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];

二、设置应用支持的屏转方向

1、在TARGET->Deployment Info->Device Orientation下勾选需要支持的屏转方向设置

这里需要注意:

Landscape的设置和device的orientation的左右是反的,和interfaceOrientation是一致的。

此处的设置如果是iPhone的话设置Upside Down是无效的,手机不支持Upside Down,即倒立方向的设置?


2、在AppDelegate的方法中设置

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window  NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED {
    
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

在AppDelegate的方法中设置后,会将1中的设置覆盖掉。

三、设置某个界面支持的屏转方向

1、如果该界面是被present出来的,则直接在该界面中试下如下方法即可生效

//当前页面是否支持屏幕旋转
- (BOOL)shouldAutorotate {
    return YES;
}
//当前页面支持的屏转方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

//从上已界面进入时的初始方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeLeft;
}

这里设置了初始方向是home键在又,支持的屏转是横屏。

如果未设置初始的屏幕方向的话,会和支持的上一个界面方向保持一致。

注意:如果该页面是被push出来的,这种设置会失效。


2、如果该页面是被push进来的,需要在navigationController和前一个后一个界面中都做处理,否则你会发现在pop回来后会发现第一个界面支持的屏转方向和第二个界面不一致的就不能旋转了。

navigationController中:

- (BOOL)shouldAutorotate {
    return self.visibleViewController.shouldAutorotate;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.visibleViewController.supportedInterfaceOrientations;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return self.visibleViewController.preferredInterfaceOrientationForPresentation;
}

前一个controller中:

这个界面只支持竖屏

//当前页面是否支持屏幕旋转
- (BOOL)shouldAutorotate {
    return YES;
}
//当前页面支持的屏转方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
//从上已界面进入时的初始方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

后一个controller中:

这个界面只支持横屏

//当前页面是否支持屏幕旋转
- (BOOL)shouldAutorotate {
    return YES;
}
//当前页面支持的屏转方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}
//从上已界面进入时的初始方向(push进来的界面这种设置无效)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeLeft;
}

从第一个界面中push到第二个界面,发现第二个界面初始的屏幕方向并不是设置的UIInterfaceOrientationLandscapeLeft,则需要强制旋转屏幕,见强制转屏内容。

四、强制转屏

- (void)rotateScreen:(UIInterfaceOrientation)orientation
{
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
    {
        NSNumber *num = [[NSNumber alloc] initWithInt:orientation];
        [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)num];
        [UIViewController attemptRotationToDeviceOrientation];
    }
    SEL selector=NSSelectorFromString(@"setOrientation:");
    NSInvocation *invocation =[NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:[UIDevice currentDevice]];
    int val = orientation;
    [invocation setArgument:&val atIndex:2];
    [invocation invoke];
}

五、可能会碰到的问题

1、iPhone旋转横屏后,状态栏去了哪里?

原来是横屏的时候被系统给隐藏了,竖屏的时候又显示出来了。在基controller中作如下处理:

//设置状态栏转屏时不隐藏
- (BOOL)prefersStatusBarHidden {
    return NO;
}

2、- preferredInterfaceOrientationForPresentation:方法执行机制是什么?

当从上一个界面到当前界面时,此方法会被调用,一次来设置当前页面初始的展示方式。

3、在viewController里面写了设置屏转的三个方法,为什么屏转控制是无效的?

从第一个界面push到第二个界面,这种情况下,第二个界面的默认方向就是前一个界面的方向。

六、闲话

一般的,如果后一个界面没有明确的设计展示方向的话,应该和它前一个界面的屏幕方向保持一致。

如果后一个界面需要转屏的话,转屏设置应该在后一个界面中完成。

对于iPad应用来说,如果同时支持横竖屏模式的话,在用户体验上会增强很多,苹果也是很支持iPad同时做好横竖屏的转换的。

demo

iOS开发中屏幕旋转(一)
iOS开发中屏幕旋转(二)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 在 iOS 开发,可以通过实现 UINavigationControllerDelegate 协议的 navigationController:didShowViewController:animated: 方法来拦截侧滑返回手势。 例如,你可以在这个方法判断当前展示的视图控制器是否需要禁用侧滑返回手势,如果需要,则设置导航控制器的 interactivePopGestureRecognizer.enabled 属性为 NO,否则设置为 YES。 示例代码如下: ``` - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { BOOL shouldEnableGesture = YES; if ([viewController conformsToProtocol:@protocol(DisablePopGestureProtocol)]) { shouldEnableGesture = [viewController disablePopGesture]; } navigationController.interactivePopGestureRecognizer.enabled = shouldEnableGesture; } ``` 在这段代码,我们新增了一个协议 DisablePopGestureProtocol,用于标记那些视图控制器需要禁用侧滑返回手势。在每个视图控制器,如果希望禁用侧滑返回手势,则只需要实现这个协议的 disablePopGesture 方法并返回 NO 即可。 希望这些信息对你有所帮助! ### 回答2: 在iOS开发,拦截侧滑返回手势的方法主要是通过自定义导航控制器或者使用UINavigationControllerDelegate的相关方法来实现。 一种方法是创建一个自定义导航控制器,并在其重写`interactivePopGestureRecognizer`的`delegate`属性。首先,我们需要创建一个继承自`UINavigationController`的子类,比如`CustomNavigationController`,然后重写其`viewDidLoad`方法,在该方法内设置`interactivePopGestureRecognizer`的`delegate`属性为自身,并实现`UIGestureRecognizerDelegate`协议,最后在`gestureRecognizerShouldBegin`方法内返回`false`。这样一来,在默认的滑动返回手势,只要是继承自这个自定义导航控制器的视图控制器都将拦截滑动返回手势,无法触发返回动作。 另一种方法是通过实现`UINavigationControllerDelegate`协议相关方法来拦截侧滑返回手势。在视图控制器设置`UINavigationController`的代理为自身,并实现`navigationController(_:willShow:animated:)`方法。在该方法内,我们可以通过修改导航控制器的`interactivePopGestureRecognizer`属性的`enabled`属性来控制是否允许滑动返回手势。当我们设置为`false`时,滑动返回手势将被拦截,无法触发返回动作。 需要注意的是,如果选择第二种方法,我们还需要在视图控制器将要消失的时候将导航控制器的代理置空,以避免内存泄漏和不必要的代理方法调用。 总结起来,要拦截侧滑返回手势,可以通过自定义导航控制器或者使用`UINavigationControllerDelegate`的相关方法,来对滑动返回手势进行监听和控制。 ### 回答3: 在iOS开发,拦截侧滑返回手势可以通过以下几个步骤实现。 首先,我们可以通过遵循UIGestureRecognizerDelegate协议,并实现它的gestureRecognizerShouldBegin方法。这个方法会在每次手势开始时被调用。 在实现gestureRecognizerShouldBegin方法时,可以通过判断当前viewController的类型或者一些其他的条件,来决定是否拦截侧滑返回手势。如果不满足拦截条件,可以返回NO,表示不拦截手势,否则返回YES。 例如,想要在特定的viewController拦截侧滑返回手势,可以按照以下方式实现: ``` extension ViewController: UIGestureRecognizerDelegate { override func viewDidLoad() { super.viewDidLoad() navigationController?.interactivePopGestureRecognizer?.delegate = self } func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { if let currentVC = navigationController?.topViewController, currentVC is MySpecialViewController { return false } return true } } ``` 在上述代码,将ViewController设置为UIGestureRecognizerDelegate的代理,然后在viewDidLoad方法,将导航控制器的interactivePopGestureRecognizer的代理设置为self,即ViewController。接着,在gestureRecognizerShouldBegin方法,判断当前导航控制器的顶层视图控制器是否是特定的MySpecialViewController,并返回相应的布尔值。 这样,如果当前在MySpecialViewController,就会拦截侧滑返回手势。 以上就是在iOS开发拦截侧滑返回手势的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Morris_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值