美文网首页
iOS 切换横竖屏

iOS 切换横竖屏

作者: Sparkle_S | 来源:发表于2017-02-27 11:42 被阅读0次

参照:iOS强制转换横竖屏和键盘方向控制

实现点击按钮切换横竖屏的功能,设备锁屏无影响。效果如图所示:

手动切换横竖屏.gif
方法一:通过setOrientation:方法
首先,要保证支持横竖屏,你需要在这里进行设置
支持横竖屏.png
其次,对需要旋转的界面进行处理:需要支持屏幕旋转,可对支持的方向进行控制
-(BOOL)shouldAutorotate{        //需要支持旋转屏幕
    return YES;    //默认YES
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{        //控制支持的方向
    return _btn.selected?UIInterfaceOrientationMaskLandscapeRight:UIInterfaceOrientationMaskPortrait;    //不希望屏幕自动旋转,通过这里控制,有时会失效。
   //return UIInterfaceOrientationMaskAllButUpsideDown;    //默认和Target中的设置相同
}

然后,就是通过利用NSInvocation调用对象的消息

/**
 切换横竖屏

 @param sender button
 */
- (IBAction)btnAction:(UIButton *)sender {
    
    if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
        
        SEL selector = NSSelectorFromString(@"setOrientation:");
        
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        
        int val = UIDeviceOrientationPortrait;//竖屏
        UIDeviceOrientation duration = [[UIDevice currentDevice]orientation];
        if (UIDeviceOrientationPortrait == duration) {
            val = UIInterfaceOrientationLandscapeLeft;//横屏
        }
        
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

补充:上面有提到,不希望屏幕自动旋转,控制时有时会失效,比如你点击了按钮之后,旋转了手机,再次点击按钮。解决办法,添加监听设备方向的通知,如果是手动触发旋转屏,则返回;如果是设备旋转触发旋转屏,则重置屏幕方向。

    //添加监听设备方向的通知
    _inteface = UIInterfaceOrientationMaskPortrait;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:)name:UIDeviceOrientationDidChangeNotification object:nil];
//监听设备方向的通知方法
- (void)orientChange:(NSNotification *)noti {
    if (_isManualRotate) {
        _isManualRotate = NO;
        return;
    }
    if (_inteface == UIInterfaceOrientationMaskPortrait) {
        if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
            SEL selector = NSSelectorFromString(@"setOrientation:");
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
            [invocation setSelector:selector];
            [invocation setTarget:[UIDevice currentDevice]];
            int val = UIInterfaceOrientationPortrait;
            [invocation setArgument:&val atIndex:2];
            [invocation invoke]; }
    }else {
        if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
            SEL selector = NSSelectorFromString(@"setOrientation:");
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
            [invocation setSelector:selector];
            [invocation setTarget:[UIDevice currentDevice]];
            int val = UIInterfaceOrientationLandscapeRight ;
            [invocation setArgument:&val atIndex:2];
            [invocation invoke];
        }
    }
}

方法二:通过设置视图的transform
首先:关闭自动旋转,否则状态栏不旋转。值得庆幸的是,界面是否支持旋转不影响效果

-(BOOL)shouldAutorotate{        //必须关闭旋转屏幕
    return NO;      //默认YES
}

然后:调用代码旋转状态栏和视图

    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    [UIView animateWithDuration:duration animations:^{
        [[UIApplication sharedApplication] setStatusBarOrientation:_btn.selected ? UIInterfaceOrientationLandscapeRight : UIInterfaceOrientationPortrait];
        self.view.transform = _btn.selected ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformIdentity;
        self.view.frame = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
    }];

期待你的评论建议O(∩_∩)O~

相关文章

  • [iOS]终极横竖屏切换解决方案

    [iOS]终极横竖屏切换解决方案 [iOS]终极横竖屏切换解决方案

  • JS 与 IOS 交互-横竖屏切换

    IOS 设备横竖屏情况 一般情形 所有界面都支持横竖屏切换如果App的所有切面都要支持横竖屏的切换,那只需要勾选【...

  • iOS 16强制切换横竖屏失效解决

    ios16切换横竖屏代码 注意:1.ios16 开始 UIDeviceOrientationDidChangeNo...

  • 跳转自动横屏

    iOS 知识小集(横竖屏切换) 转载自 http://www.cocoachina.com/ios/2016072...

  • IOS 横竖屏

    //iOS规定不允许强制用代码切换横竖屏 if([[UIDevicecurrentDevice]respondsT...

  • iOS16适配:APP切换横竖屏问题

    iOS16之前切换横竖屏使用的是UIDevice的setValue:forKey:方法进行切换。但在iOS16后不...

  • iOS 横竖屏切换

    AppDelegate.h @property(nonatomic,assign)BOOL isAllowRota...

  • iOS 横竖屏切换

    1、打开横竖屏开关 2、因为屏幕翻转后width和height是相反的 github地址:https://gith...

  • iOS 横竖屏切换

    iOS 中横竖屏切换的功能,在开发iOS app中总能遇到。以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰...

  • iOS 横竖屏切换

    本文只针对单个界面横竖屏界面 1.首先在TARGETS中将横竖屏勾选上(不用勾选貌似也可以,只不过需要在AppDe...

网友评论

      本文标题:iOS 切换横竖屏

      本文链接:https://www.haomeiwen.com/subject/gckzwttx.html