2015-09-12 42 views
4

我有一個簡單的應用程序組成的單個視圖控制器。我開始用Xcode的7 GM單一視圖的應用模板,但隨後被刪除的主要情節串連圖板,併成立了我的視圖控制器是這樣的:shouldAutorotate不呼叫視圖控制器

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    let vc = ViewController() 

    window = UIWindow(frame: UIScreen.mainScreen().bounds) 
    window!.rootViewController = vc 
    window!.makeKeyAndVisible() 

    return true 
} 

在我的信息的plist,我已經在指定的所有定向支持的接口方向,並且該應用可以在iPad上旋轉至所有方向。

但是,在我的簡單視圖控制器中,shouldAutorotate()supportedInterfaceOrientations()方法從不會被調用。這是一個問題,因爲我正在嘗試啓用和禁用自動旋轉的UI控件。有什麼可以阻止這些方法被調用?

示例項目here(需要斯威夫特2)


¹non-UINavigationController

回答

5

根據這個職位上的蘋果開發者論壇,如果你啓用了iPad的多任務處理(新iOS中9),你再也無法控制的方向,你支持:

https://forums.developer.apple.com/message/13508#13508

您可以廣告d一個反轉,但它不漂亮,至少據我所知。我知道它的工作原理,但是當你旋轉時無法禁用角落的動畫,所以你看到它看起來像是在旋轉,但內容不旋轉。

這是我用來對抗旋轉的代碼。請注意,我必須隱藏狀態欄,否則它也會旋轉,我無法弄清楚如何解決這個問題。

另請注意,在self.navigationController.view.superview.superview上執行自動旋轉可能不是最佳方式,並且可能在未來的某個時間點中斷。有可能有更好的方法來獲得用於抵消旋轉的正確視圖。顯然,如果您不使用導航控制器,則需要傳入不同的視圖。因人而異。

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    [[UIApplication sharedApplication] setStatusBarHidden:YES]; 
    self.startingInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
} 

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; 
    [self updateLayoutsForCurrentOrientation:toInterfaceOrientation view:self.navigationController.view.superview.superview]; 
} 

- (void)updateLayoutsForCurrentOrientation:(UIInterfaceOrientation)toInterfaceOrientation view:(UIView *)view { 
    CGAffineTransform transform = CGAffineTransformIdentity; 

    if (self.startingInterfaceOrientation == UIInterfaceOrientationPortrait) { 
     switch (toInterfaceOrientation) { 
      case UIInterfaceOrientationLandscapeLeft: 
       transform = CGAffineTransformMakeRotation(M_PI/2.0f); 
       break; 
      case UIInterfaceOrientationLandscapeRight: 
       transform = CGAffineTransformMakeRotation(-M_PI/2.0f); 
       break; 
      case UIInterfaceOrientationPortrait: 
       transform = CGAffineTransformIdentity; 
       break; 
      case UIInterfaceOrientationPortraitUpsideDown: 
       transform = CGAffineTransformMakeRotation(M_PI); 
       break; 
      default: 
       break; 
     } 
    } 
    else if (self.startingInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
     switch (toInterfaceOrientation) { 
      case UIInterfaceOrientationLandscapeLeft: 
       transform = CGAffineTransformMakeRotation(-M_PI/2.0f); 
       break; 
      case UIInterfaceOrientationLandscapeRight: 
       transform = CGAffineTransformMakeRotation(M_PI/2.0f); 
       break; 
      case UIInterfaceOrientationPortrait: 
       transform = CGAffineTransformMakeRotation(M_PI); 
       break; 
      case UIInterfaceOrientationPortraitUpsideDown: 
       transform = CGAffineTransformIdentity; 
       break; 
      default: 
       break; 
     } 
    } 
    else if (self.startingInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) { 
     switch (toInterfaceOrientation) { 
      case UIInterfaceOrientationLandscapeLeft: 
       transform = CGAffineTransformIdentity; 
       break; 
      case UIInterfaceOrientationLandscapeRight: 
       transform = CGAffineTransformMakeRotation(M_PI); 
       break; 
      case UIInterfaceOrientationPortrait: 
       transform = CGAffineTransformMakeRotation(-M_PI/2.0f); 
       break; 
      case UIInterfaceOrientationPortraitUpsideDown: 
       transform = CGAffineTransformMakeRotation(M_PI/2.0f); 
       break; 
      default: 
       break; 
     } 
    } 
    else if (self.startingInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
     switch (toInterfaceOrientation) { 
      case UIInterfaceOrientationLandscapeLeft: 
       transform = CGAffineTransformMakeRotation(M_PI); 
       break; 
      case UIInterfaceOrientationLandscapeRight: 
       transform = CGAffineTransformIdentity; 
       break; 
      case UIInterfaceOrientationPortrait: 
       transform = CGAffineTransformMakeRotation(M_PI/2.0f); 
       break; 
      case UIInterfaceOrientationPortraitUpsideDown: 
       transform = CGAffineTransformMakeRotation(-M_PI/2.0f); 
       break; 
      default: 
       break; 
     } 
    } 
    view.transform = transform; 
} 

大部分代碼是從賈裏德·辛克萊的JTSImageViewController適應(貼有他的許可),可在MIT許可下在GitHub上的位置:https://github.com/jaredsinclair/JTSImageViewController

+0

謝謝。我可能實際上能夠幫助你進行角球輪換。在'-viewWillTransitionToSize:withTransitionCoordinator:'中調用'[UIView setAnimationsEnabled:NO];',然後'[coordinator animateAlongsideTransition:nil completion:...',並在完成時調用'[UIView setAnimationsEnabled:YES];' –

+0

謝謝。我喜歡Apple的文檔如何∞ – SpaceDog

0

要選擇不符合參與幻燈片和拆分視圖的資格,請將UIRequiresFullScreen鍵添加到您的Xcode項目的Info.plist文件中並應用布爾值YES。