2014-06-05 59 views
113

我試圖實現已在iOS 8中引入的新的viewWillTransitionToSize方法(所有其他旋轉方法已被棄用)。我想知道didRotateFromInterfaceOrientation的等價物是什麼,因爲我們需要執行一些清理任務,並且我看不到我們可以分配給UIViewControllerTransitionCoordinator以便在「過渡」時調用的塊一個新的尺寸完成。謝謝。棄用循環方法,等同於'didRotateFromInterfaceOrientation'?

回答

214

好的發現它,只需要在傳遞的UIViewControllerTransitionCoordinator上使用animateAlongsideTransition:completion:方法。

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator 
{ 
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) 
    { 
     UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
     // do whatever 
    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) 
    { 

    }]; 

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; 
} 
+0

每次我們像didRotateFromInterfaceOrientation一樣旋轉時,這個方法會調用嗎?你可以請我分享你使用的方法。謝謝 – ask123

+2

是的。這是我做: ' - (無效)viewWillTransitionToSize:(CGSize)大小withTransitionCoordinator:(ID )協調{ [協調animateAlongsideTransition:^(ID 上下文){ UIInterfaceOrientation取向= [ [UIApplication sharedApplication] statusBarOrientation]; //做任何事 }完成:^(id context){ }]; [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; }' – strange

+0

謝謝你:)請試試這個 – ask123

65

答案的雨燕版本奇怪的

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { 

    coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -> Void in 

     let orient = UIApplication.sharedApplication().statusBarOrientation 

     switch orient { 
     case .Portrait: 
      println("Portrait") 
      // Do something 
     default: 
      println("Anything But Portrait") 
      // Do something else 
     } 

     }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in 
      println("rotation completed") 
    }) 

    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator) 
} 
+0

這很奇怪,因爲我把它放在我的ViewController中,它沒有'打印'任何東西到我的日誌。我猜是因爲這個方法沒有被調用。你能想到其他任何需要插入才能使其工作的東西嗎? – Trip

+0

我只是將它添加到一個空的項目(通用應用程序),它工作正常,無需添加任何東西。也許在方法的一開始就寫一個日誌語句,看看它是否被調用。我想不出任何你需要添加的東西。你在運行什麼iOS? – DogCoffee

+0

啊!它工作得很好。非常感謝! – Trip

3

[[UIApplication sharedApplication] statusBarOrientation]在iOS9已經過時,你必須測試針對UITraitCollection各種設備。

override func willTransitionToTraitCollection(newCollection: UITraitCollection, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { 

    if newCollection.containsTraitsInCollection(UITraitCollection(verticalSizeClass: .Regular)) { 
     ... 
    } 
    } 
+6

只有setter已被棄用。一位蘋果員工發佈說:「閱讀狀態欄的方向並不被棄用,只能寫入它,如果你在getter上看到這個,這可能是我們構建標題的錯誤。」 (https://forums.developer.apple.com/thread/12937) –

+0

雖然也不贊成getter。根據[文檔](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instp/UIApplication/statusBarOrientation)它是。 – Groot

3

由於問題是:什麼是的didRotateFromInterfaceOrientation

相當於我想我會貢獻代碼如下:

@implementation ViewController 
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { 
    [super traitCollectionDidChange:previousTraitCollection]; 
    if (previousTraitCollection.verticalSizeClass == UIUserInterfaceSizeClassRegular) { 
     NSLog(@"User has rotated to landscape"); 
    } else if (previousTraitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) { 
     NSLog(@"User has rotated to portrait"); 
    } 
} 
@end 

我在模擬器上的iPhone進行測試,但如果使用iPad進行測試,我的打印語句將無法運行,因爲traitsCollection不會更改。

這很奇怪,因爲這正是蘋果recommends

- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection { 
    [super traitCollectionDidChange: previousTraitCollection]; 
    if ((self.traitCollection.verticalSizeClass != previousTraitCollection.verticalSizeClass) 
     || self.traitCollection.horizontalSizeClass != previousTraitCollection.horizontalSizeClass)) { 
     // your custom implementation here 
    } 
} 
+0

第二個代碼段中缺少括號 –

+0

這是否適用於全屏幕上垂直和水平類都爲Regular的iPad? –

6

斯威夫特3接受的答案:

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) { 
    coordinator.animate(alongsideTransition: { (_) in 
     let orient = UIApplication.shared.statusBarOrientation 

     switch orient { 
     case .portrait: 
      print("Portrait") 
     // Do something 
     default: 
      print("Anything But Portrait") 
      // Do something else 
     } 
    }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in 
     print("rotation completed") 
    }) 

    super.willTransition(to: newCollection, with: coordinator) 
} 

它工作正常,我

+0

在iOS 10上不適用於我 - 它打印舊的方向,而不是新的方向。 –

+1

@VaddadiKartick因爲你應該使用'讓東方= newCollection.verticalSizeClass 開關東方{ 情況下.compact: 打印( 「Lanscape」) //做一些 默認: 打印( 「縱向」) //完成'別的東西 }' –

6

iOS的10.3 & Swift 3

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) { 

     coordinator.animate(alongsideTransition: { (_) in 
      let orient = newCollection.verticalSizeClass 

      switch orient { 
      case .compact: 
       print("Lanscape")///Excluding iPads!!! 

      default: 
       print("Portrait") 
      } 
     }, completion: { _ in 
      print("rotation completed") 
     }) 

     super.willTransition(to: newCollection, with: coordinator) 
    }