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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24071453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:34:07  来源:igfitidea点击:

Rotation methods deprecated, equivalent of 'didRotateFromInterfaceOrientation'?

iosiphoneobjective-cios8

提问by strange

I'm trying to implement the new viewWillTransitionToSizemethod which has been introduced in iOS 8 (all other rotation methods have been deprecated). I'd like to know what the equivalent of didRotateFromInterfaceOrientationis now as there are a number of clean up tasks we need to perform and I can't see a block that we can assign to UIViewControllerTransitionCoordinatorin order to be called when 'transition' to a new size finishes. Thanks.

我正在尝试实现viewWillTransitionToSizeiOS 8 中引入的新方法(所有其他旋转方法已被弃用)。我想知道现在的等价物didRotateFromInterfaceOrientation是什么,因为我们需要执行许多清理任务,而且我看不到我们可以分配给的块,UIViewControllerTransitionCoordinator以便在“转换”到新的尺寸完成。谢谢。

回答by strange

Okay found it, just have to use the animateAlongsideTransition:completion:method on the passed UIViewControllerTransitionCoordinator.

好的找到了,只需要animateAlongsideTransition:completion:在传递的UIViewControllerTransitionCoordinator.

- (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];
}

回答by DogCoffee

The Swift Version of the answer by strange

奇怪的答案的 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)
}

回答by Mike Glukhov

iOS 10.3 & Swift 3

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

回答by j_gonfer

The accepted answer in Swift 3:

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

It works fine for me

这对我来说可以

回答by NYC Tech Engineer

Since the question was: what was the equivalent of didRotateFromInterfaceOrientation

因为问题是:什么是等价物 didRotateFromInterfaceOrientation

I thought I'd contribute the code below:

我想我会贡献下面的代码:

@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

I was testing on an iPhone in the simulator, but my print statements won't get run if I test using the iPad since the traitsCollection won't change.

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

This is strange because this is exactly what Apple recommends:

这很奇怪,因为这正是 Apple推荐的

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

回答by Yannick

[[UIApplication sharedApplication] statusBarOrientation]is deprecated in iOS9 you have to test against UITraitCollectionfor various devices.

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

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

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

回答by Avi Pogrow

On the Ipad there is no trait collection change so here is how you detect the rotation from start and completion. Here is the Swift 5 syntax:

在 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")


    }
}