iOS:如何在设备旋转后运行函数 (Swift)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26943808/
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 03:38:07  来源:igfitidea点击:

iOS: How to run a function after Device has Rotated (Swift)

iosswiftrotation

提问by Richard

I have one UIView which is not using Auto-Layout and some components are displayed based on their percent of X and Y co-ordinates from the main view.

我有一个不使用自动布局的 UIView,一些组件是根据主视图中 X 和 Y 坐标的百分比显示的。

Previously I would have run a function to update their positions in didRotateFromInterfaceOrientation however I see this is now deprecated in iOS8.

以前我会运行一个函数来更新他们在 didRotateFromInterfaceOrientation 中的位置,但是我发现这在 iOS8 中现在已被弃用。

I've taken a look at viewWillTransitionToSize but it's giving weird results, and there doesn't appear to be a viewDidtransitionToSize function.

我查看了 viewWillTransitionToSize 但它给出了奇怪的结果,而且似乎没有 viewDidtransitionToSize 函数。

Is there an easy way (in Swift) to run a function after a device rotation?

有没有一种简单的方法(在 Swift 中)在设备旋转后运行一个函数?

回答by Acey

The viewWillTransitionToSizedelegate method gets called with a UIViewControllerTransitionCoordinatorconforming object. A method that protocol declares is animateAlongsideTransition(_:animation, completion:). You can use that to have code execute after the transition is complete.

viewWillTransitionToSize委托方法被调用了UIViewControllerTransitionCoordinator符合对象。协议声明的方法是animateAlongsideTransition(_:animation, completion:). 您可以使用它在转换完成后执行代码。

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
    coordinator.animateAlongsideTransition(nil, completion: {
        _ in

        // Your code here
    })
}

回答by daxh

Above answer https://stackoverflow.com/a/26944087/6583492is absolutely right, thanks to Acey and Moonwalkr. But for swift 3.0 it will looks like the following

以上答案https://stackoverflow.com/a/26944087/6583492绝对正确,感谢 Acey 和 Moonwalkr。但是对于 swift 3.0,它将如下所示

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    coordinator.animate(alongsideTransition: nil) { _ in
        // Your code here
    }
}

回答by Olivier de Jonge

Although not asked for here Objective C version:

虽然这里没有要求 Objective C 版本:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {

    // change any properties on your views

} completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if( UIDeviceOrientationIsPortrait(orientation) ) {
        NSLog(@"portrait");
    } else {
        NSLog(@"landscape");
    }  
}];
}