您好,登录后才能下订单哦!
这篇文章主要介绍“iOS如何实现手动和自动屏幕旋转”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“iOS如何实现手动和自动屏幕旋转”文章能帮助大家解决问题。
首先iPhone中屏幕分为状态栏方向和设备方向
typedef NS_ENUM(NSInteger, UIDeviceOrientation) { UIDeviceOrientationUnknown, UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left UIDeviceOrientationFaceUp, // Device oriented flat, face up UIDeviceOrientationFaceDown // Device oriented flat, face down }; typedef NS_ENUM(NSInteger, UIInterfaceOrientation) { UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown, UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight, UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft };
系统提供两个地方来设置设备的方向,取两个地方的交集是最后的设备所支持的方向
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window; -(NSUInteger)supportedInterfaceOrientations;
这里需要注意的是返回的时下面的枚举
typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) { UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait), UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft), UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight), UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown), UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown), UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), };
在转动屏幕的时候会触发下面方法
-(BOOL)shouldAutorotate;
在该方法返回真,自动调用上面的两个方法得到方向。
修改状态栏方向的方法
1、使用私有API setOrientation;
2、修改状态栏的方向,并通过设置View的transform来达到伪旋转的结果,但是设备方向并没有改变
3、主动出发系统支持的方法,就相当于让这个vc在重新出来的时候系统判断所支持的方向的机制重新走一遍。
- (void)awakeSupportInterOrtation:(UIViewController *)showVC completion:(void(^)(void))block { UIViewController *vc = [[UIViewController alloc] init]; void(^completion)() = ^() { [showVC dismissViewControllerAnimated:NO completion:^{ if (block) { block(); } }]; }; // This check is needed if you need to support iOS version older than 7.0 BOOL canUseTransitionCoordinator = [showVC respondsToSelector:@selector(transitionCoordinator)]; if (canUseTransitionCoordinator) { [showVC presentViewController:vc animated:NO completion:nil]; [showVC.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) { completion(); }]; } else { [showVC presentViewController:vc animated:NO completion:completion]; } } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } -(BOOL)shouldAutorotate { return YES; }
在需要转为竖屏的时候调用一个方法,在后面两个方法中如上实现,第二个方法中返回的是你最终要转向的方向。
关于“iOS如何实现手动和自动屏幕旋转”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。