不推荐使用旋转方法,相当于“ did RotateFromInterfaceDirecting”?

我正在尝试实现 iOS8中引入的新的 viewWillTransitionToSize方法(所有其他的旋转方法都已被废弃)。我想知道现在的 didRotateFromInterfaceOrientation是什么,因为有许多清理任务,我们需要执行,我不能看到一个块,我们可以分配给 UIViewControllerTransitionCoordinator,以便调用时,“过渡”到一个新的大小完成。谢谢。

62835 次浏览

找到了,只需要对传递的 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];
}

迅速的答案由奇怪的版本

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

在 iOS9中不推荐使用 [[UIApplication sharedApplication] statusBarOrientation],您必须针对各种设备对 UITraitCollection进行测试。

  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

我正在模拟器中测试 iPhone,但是如果我使用 iPad 进行测试,我的 print 语句将无法运行,因为 traitsCollection 不会改变。

这很奇怪,因为这正是苹果 建议:

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

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

在 Ipad 上没有特质收集的更改,所以下面是如何检测从开始到完成的旋转。以下是 Swift 5的语法:

override func viewWillTransition(to size: CGSize, with coordinator:
UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)


coordinator.animate(alongsideTransition: { [unowned self] _ in


self.view.backgroundColor = UIColor.blue
print("rotation in progress")


}) { [unowned self] _ in
self.view.backgroundColor = UIColor.green
print("rotation complete")




}
}