iOS: How to run a function after Device has Rotated (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)


- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- android 4中的android RadioButton问题 2022-01-01