[英]iOS viewWillTransitionToSize and device orientation
我正在使用viewWillTransitionToSize來檢測設備何時旋轉到橫向。 根據目標大小,我可以檢測是否前往景觀並根據需要調整我的特征......
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
if size.width > size.height {
self.setOverrideTraitCollection(UITraitCollection(horizontalSizeClass: UIUserInterfaceSizeClass.Regular), forChildViewController: viewController)
}
else{
self.setOverrideTraitCollection(nil, forChildViewController: viewController)
}
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}
但是,我希望能夠檢測我的設備是轉換到橫向左側還是橫向右側。 這將允許我根據設備的具體方向創建不同的行為或視圖。 (左還是右)。
這可能不使用任何已棄用的函數嗎?
我想過使用狀態欄方向......
let orientation = UIApplication.sharedApplication().statusBarOrientation;
if( orientation == UIInterfaceOrientation.LandscapeLeft )
{
// Do something
}
else if( orientation == UIInterfaceOrientation.LandscapeRight )
{
// Do something else
}
...但這沒有用,因為這似乎給出了“舊的”狀態方向。
如何獲得特定的目標定位?
您可以在旋轉時使用UIViewControllerTransitionCoordinator
獲取方向,如下所示:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
// will execute before rotation
coordinator.animate(alongsideTransition: { (context: UIViewControllerTransitionCoordinatorContext) in
// will execute during rotation
let orientation = UIApplication.shared.statusBarOrientation
if orientation == .landscapeLeft
{
// Do something
}
else if orientation == .landscapeRight
{
// Do something else
}
}) { (context: UIViewControllerTransitionCoordinatorContext) in
// will execute after rotation
}
}
使用[UIDevice currentDevice].orientation
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.