iOS 強制橫屏、部分橫屏等功能實踐

需求如下:

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

需求1解決方法:

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

屏幕快照 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點擊)事件處,代碼如下



- (void)fullScreenBtnAction {
    if (_isNormalOrientation) {   // 如果當(dāng)前是默認的豎屏
        //注:當(dāng)前self是UIView對象,如果是VC,則為self.view.....
        self.frame = CGRectMake(0, 0, kScreenSize.height, kScreenSize.width);
        CGRect frame = [UIScreen mainScreen].applicationFrame;
        // transfrom會以當(dāng)前center為錨點旋轉(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)時間
//        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是進入橫屏前self的frame
        self.frame = _originRect;
    }
    _isNormalOrientation = !_isNormalOrientation; //更改狀態(tài)
}

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


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

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

@property (assign , nonatomic)UIInterfaceOrientation interfaceOrientation;

在appdelegate的.m文件中實現(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];
}




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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • iOS 知識小集(橫豎屏切換) 轉(zhuǎn)載自 http://www.cocoachina.com/ios/2016072...
    Poison_19ce閱讀 703評論 0 0
  • 當(dāng)iphone的某一方向(一般是豎屏)鎖定沒有開啟的時候,當(dāng)手機橫放后,沒有寫死程序支持方向的程序或者支持橫屏的程...
    SuperZico閱讀 8,891評論 0 4
  • 概述 寫代碼就是在不斷填坑的過程中慢慢成長,程序員哪有不遇坑的呢? 這篇文章來談?wù)刬OS中橫豎屏切換的一些坑,橫豎...
    jumpingfrog0閱讀 11,487評論 6 21
  • iOS 中橫豎屏切換的功能,在開發(fā)iOS app中總能遇到。以前看過幾次,感覺簡單,但是沒有敲過代碼實現(xiàn),最近又碰...
    零度_不結(jié)冰閱讀 2,300評論 0 0
  • 對于在一年中大部分的時間都獨自一人生活的我來說,孤獨是常有的事情。不善社交,也疲于社交,于是在微信中看一些有趣的帖...
    Jeffery_yu閱讀 190評論 0 0

友情鏈接更多精彩內(nèi)容