iOS 強(qiáng)制橫屏、部分橫屏等功能實(shí)踐

需求如下:

1.app整體只能豎屏,部分頁面才可以橫屏
2.app整體只能豎屏,部分頁面也是豎屏,但是點(diǎn)擊某個(gè)按鈕可以使當(dāng)前頁面變?yōu)闄M屏,如全屏視頻播放鍵。

需求1解決方法:

1.在targets - general中設(shè)置設(shè)備支持方向如下,確保設(shè)備各個(gè)方法均支持

屏幕快照 2016-12-08 下午6.41.26.png

2.在appdelegate的.h文件聲明屬性來標(biāo)記當(dāng)前設(shè)備方向

@property (assign , nonatomic)UIInterfaceOrientation interfaceOrientation;

在appdelegate的.m文件中:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
    
    if(self.interfaceOrientation == UIInterfaceOrientationUnknown) {
 // 直播間用
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else if(self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
//交易頁面橫屏用
        return UIInterfaceOrientationMaskLandscapeRight;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

3.在需要橫屏的ViewController的ViewWillApper方法里面:

 

 Appdelegate *delegate =p.p1[UIApplication sharedApplication].delegate;
delegate.canRotate = YES;

需求2解決方法:

方法1:
1.在targets - general中設(shè)置設(shè)備支持方向如下,確保整體都是豎屏

屏幕快照 2016-12-08 下午6.30.25.png

2.在橫屏(如button點(diǎn)擊)事件處,代碼如下



- (void)fullScreenBtnAction {
    if (_isNormalOrientation) {   // 如果當(dāng)前是默認(rèn)的豎屏
        //注:當(dāng)前self是UIView對(duì)象,如果是VC,則為self.view.....
        self.frame = CGRectMake(0, 0, kScreenSize.height, kScreenSize.width);
        CGRect frame = [UIScreen mainScreen].applicationFrame;
        // transfrom會(huì)以當(dāng)前center為錨點(diǎn)旋轉(zhuǎn),所以旋轉(zhuǎn)后位置有偏移,需要處理
        CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2));
        self.center = center;       
        //取狀態(tài)欄旋轉(zhuǎn)時(shí)間
//        CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration: 0.3];
        self.transform = CGAffineTransformMakeRotation(M_PI_2);
        [UIView commitAnimations];
    } else {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration: 0.3];
        self.transform = CGAffineTransformIdentity;
        [UIView commitAnimations];
        //_originRect是進(jìn)入橫屏前self的frame
        self.frame = _originRect;
    }
    _isNormalOrientation = !_isNormalOrientation; //更改狀態(tài)
}

***方法2:
1.在targets - general中設(shè)置設(shè)備支持方向如下,確保設(shè)備各個(gè)方法均支持 (棄用 可不設(shè)置)


屏幕快照 2016-12-08 下午6.41.26.png

2.在appdelegate的.h文件聲明屬性interfaceOrientation標(biāo)記方向

@property (assign , nonatomic)UIInterfaceOrientation interfaceOrientation;

在appdelegate的.m文件中實(shí)現(xiàn)設(shè)備方向方法:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
    
    if(self.interfaceOrientation == UIInterfaceOrientationUnknown) { // 直播間用
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else if(self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) { //交易頁面橫屏用
        return UIInterfaceOrientationMaskLandscapeRight;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}
  1. 橫豎屏轉(zhuǎn)換:
//轉(zhuǎn)換到豎屏
- (void)rotateToPortraitScreenWithInvocation {
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    SEL selector = NSSelectorFromString(@"setOrientation:");
    
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
    
    [invocation setSelector:selector];
    
    [invocation setTarget:[UIDevice currentDevice]];
    
    int val = UIDeviceOrientationPortrait;
    
    [invocation setArgument:&val atIndex:2];
    
    [invocation invoke];
    
    delegate.interfaceOrientation = UIInterfaceOrientationPortrait;
}

//轉(zhuǎn)換到橫屏模式
- (void)rotateToLandscapWithIncovation {
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    delegate.interfaceOrientation = UIInterfaceOrientationUnknown;
    
    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];
}




補(bǔ)充后來自己又折騰的幾個(gè)Demo:
https://github.com/3KK3/AppdelegateMethodHorizDemo/tree/master

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • iOS 知識(shí)小集(橫豎屏切換) 轉(zhuǎn)載自 http://www.cocoachina.com/ios/2016072...
    Poison_19ce閱讀 671評(píng)論 0 0
  • 1.監(jiān)聽屏幕旋轉(zhuǎn)方向 在處理iOS橫豎屏?xí)r,經(jīng)常會(huì)和UIDeviceOrientation、UIInterface...
    彬至睢陽閱讀 2,547評(píng)論 1 6
  • 當(dāng)iphone的某一方向(一般是豎屏)鎖定沒有開啟的時(shí)候,當(dāng)手機(jī)橫放后,沒有寫死程序支持方向的程序或者支持橫屏的程...
    SuperZico閱讀 8,752評(píng)論 0 4
  • 概述 寫代碼就是在不斷填坑的過程中慢慢成長,程序員哪有不遇坑的呢? 這篇文章來談?wù)刬OS中橫豎屏切換的一些坑,橫豎...
    jumpingfrog0閱讀 11,366評(píng)論 6 21
  • iOS 中橫豎屏切換的功能,在開發(fā)iOS app中總能遇到。以前看過幾次,感覺簡(jiǎn)單,但是沒有敲過代碼實(shí)現(xiàn),最近又碰...
    零度_不結(jié)冰閱讀 2,215評(píng)論 0 0