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

iOS: How to run a function after Device has Rotated (Swift)(iOS:设备旋转后如何运行函数(Swift))

本文介绍了iOS:设备旋转后如何运行函数(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个未使用自动布局的 UIView,并且某些组件根据其在主视图中的 X 和 Y 坐标百分比显示.

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.

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

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

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

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

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

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

推荐答案

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

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

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

这篇关于iOS:设备旋转后如何运行函数(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iOS:设备旋转后如何运行函数(Swift)

基础教程推荐