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)


基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01