你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS開辟中掌握屏幕扭轉的編寫辦法小結

iOS開辟中掌握屏幕扭轉的編寫辦法小結

編輯:IOS開發綜合

在IOS5.1 和 之前的版本中, 我們平日應用 shouldAutorotateToInterfaceOrientation: 來零丁掌握某個UIViewController的旋屏偏向支撐,好比:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation == UIInterfaceOrientationPortrait); 

然則在IOS6中,這個辦法被放棄了,應用有效。
shouldAutorotateToInterfaceOrientation:
Returns a Boolean value indicating whether the view controller supports the specified orientation. (Deprecated in IOS 6.0. Override the supportedInterfaceOrientations andpreferredInterfaceOrientationForPresentation methods instead.)

理論後會發明,經由過程supportedInterfaceOrientations的零丁掌握是沒法鎖定屏幕的。

-(NSUInteger)supportedInterfaceOrientations 

    return UIInterfaceOrientationMaskPortrait; 

屢次試驗後總結出掌握屏幕扭轉支撐偏向的辦法以下:
子類化UINavigationController,增長辦法

- (BOOL)shouldAutorotate 

    return self.topViewController.shouldAutorotate; 

 
- (NSUInteger)supportedInterfaceOrientations 

    return self.topViewController.supportedInterfaceOrientations; 


而且設定其為法式進口,或指定為 self.Window.rootViewController
隨後添加本身的view controller,假如想制止某個view controller的旋屏:(支撐全體版本的掌握)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation == UIInterfaceOrientationPortrait); 

 
-(BOOL)shouldAutorotate 

    return NO; 

 
-(NSUInteger)supportedInterfaceOrientations 

    return UIInterfaceOrientationMaskPortrait; 

假如想又開啟某個view controller的全體偏向旋屏支撐:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

 
-(NSUInteger)supportedInterfaceOrientations 

    return UIInterfaceOrientationMaskAllButUpsideDown; 

 
-(BOOL)shouldAutorotate 

    return YES; 

從而完成了對每一個view controller的零丁掌握。

趁便提一下,假如全部運用一切view controller都不支撐旋屏,那末爽性:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

     return UIInterfaceOrientationMaskPortrait; 


反正屏切換,視圖亂了怎樣辦?
起首,我們必需懂得一下以下4種狀況,它們被用來描寫裝備扭轉偏向:

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615451421.png (408×178)

關於旋屏的處置,年夜致分為以下幾種情形和思緒:
或許,你不須要旋屏支撐,而願望鎖定屏幕

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return NO; 


或許,你須要支撐旋屏,或許支撐部門偏向的旋屏

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  
       return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

或許,你的view有張配景圖,旋屏時體系贊助你拉伸了圖片,然則卻沒有管你的其它部件,好比button,你願望直接轉變button的年夜小和地位

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) { 
        NSLog(@"如今是豎屏"); 
        [btn setFrame:CGRectMake(213, 442, 340, 46)]; 
    } 
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { 
        NSLog(@"如今是橫屏"); 
        [btn setFrame:CGRectMake(280, 322, 460, 35)]; 
    } 


或許,你其實不願望用相對坐標去束縛控件,而是願望讓它經由過程扭轉本身順應屏幕的扭轉

- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    UIDevice *device = [UIDevice currentDevice];  
    [device beginGeneratingDeviceOrientationNotifications]; 
    //應用 NSNotificationCenter 取得扭轉旌旗燈號 UIDeviceOrientationDidChangeNotification 
    NSNotificationCenter *ncenter = [NSNotificationCenter defaultCenter];  
    [ncenter addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:device]; 

 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

 
-(void)rotation_btn:(float)n 

    UIButton *robtn = self.btn; 
    robtn.transform = CGAff.netransformMakeRotation(n*M_PI/180.0); 

 
-(void)orientationChanged 

    UIDeviceOrientation orientaiton = [[UIDevice currentDevice] orientation]; 
     
    switch (orientaiton) { 
        caseUIDeviceOrientationPortrait:              
            [self  rotation_btn:0.0]; 
            break; 
        caseUIDeviceOrientationPortraitUpsideDown:   
            [self  rotation_btn:90.0*2]; 
            break; 
        caseUIDeviceOrientationLandscapeLeft:      
            [self  rotation_btn:90.0*3]; 
            break; 
        caseUIDeviceOrientationLandscapeRight:   
            [self  rotation_btn:90.0]; 
            break; 
        default: 
            break; 
    } 


或許,你須要autoresizesSubviews = YES
或許,你願望反正屏有分歧的結構後果,須要預備2份Subview,在分歧狀況去調換
固然不要忘卻,須要調理設定圖示中的1、2處,

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615451481.png (258×296)

來贊助我們完成本身想要的順應後果。Example 動畫出現的很清楚,^_^ 我就不再煩瑣了。

【iOS開辟中掌握屏幕扭轉的編寫辦法小結】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved