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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)