iOS:设备旋转(快速)后如何运行功能 [英] iOS: How to run a function after Device has Rotated (Swift)

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

问题描述

我有一个不使用自动布局的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?

推荐答案

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:设备旋转(快速)后如何运行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆