不推荐使用循环方法,相当于“didRotateFromInterfaceOrientation”?

我试图实现iOS 8中引入的新viewWillTransitionToSize方法(所有其他旋转方法已被弃用)。 我想知道什么相当于didRotateFromInterfaceOrientation现在是因为有一些我们需要执行的清理任务,我看不到一个块,我们可以分配给UIViewControllerTransitionCoordinator为了被称为​​'过渡'时一个新的尺寸完成。 谢谢。

好的发现它,只需要在传递的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]; } 

Swift版本的答案很奇怪

 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) } 

Swift 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) } 

它适用于我👍

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) } 

[[UIApplication sharedApplication] statusBarOrientation]在iOS9中已被弃用,您必须针对各种设备对UITraitCollection进行testing。

  override func willTransitionToTraitCollection(newCollection: UITraitCollection, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { if newCollection.containsTraitsInCollection(UITraitCollection(verticalSizeClass: .Regular)) { ... } } 

由于问题是:什么是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 

我正在模拟器中testingiPhone,但是如果使用iPad进行testing,我的打印语句将无法运行,因为traitsCollection不会更改。

这很奇怪,因为这正是苹果推荐的 :

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