gpt4 book ai didi

Swift - View Controller 之间向前和向后的动画

转载 作者:行者123 更新时间:2023-11-28 07:30:01 25 4
gpt4 key购买 nike

我正在使用 Swift 4.0 设计一个 iOS 应用程序。我目前有两个 View Controller ,其中有一个 UIView 波浪图像,宽 1606,高 628:

Waves Picture

在第一个 View Controller 中,波浪位于屏幕底部:

Initial Position

当我向上滑动时,我有以下通过 segue 传递的数据:

let waveHeight = hundred?.layer.presentation()?.position.y // Current height of waves. This is a global variable

let nextViewController = segue.destination as! Secondary // nextViewController is the segue destination

nextViewController.initialXPosition = nextXPosition // this will be the initial state of the waves in the secondary view controller. Also a global variable

为了实现无缝过渡,我需要在辅助 View Controller 中准确地从它在初始 View Controller 中停止的位置开始波浪。产品看起来像这样:

https://giphy.com/gifs/2A4bUGE9UFUrG9RHPG

继续前进,当我向上滑动时,波浪需要相应地向上移动(100 个单位)。为此,我使用了以下方法:

UIView.animate(withDuration: 1, delay: 0, options: [.curveLinear], animations: {
self.hundred.transform = CGAffineTransform(translationX: 0, y: -100)

self.waveHeight = self.hundred?.layer.presentation()?.position.y
}, completion: nil)

此时,波浪在屏幕上平滑移动,向上滑动 100 个单位。

完美。

现在向下滑动是出现问题的地方。我只是想用同样的方法顺利回到初始画面;存储当前的波浪高度,通过 segue 将其传回初始 View Controller ,并让波浪向下平移 100 个单位。

这行不通。

出于某种原因,我无法在第二个 View Controller 中存储波浪的 y 位置。我设置了打印语句,无论向下滑动时我在动画中的什么位置,控制台每次都会返回相同的值。因此,当我返回初始 View Controller 时,波浪会自动前进到屏幕底部。它非常不稳定。

有没有更好的方法在 View Controller 之间来回传递动画?

最佳答案

与其尝试在 2 个 View Controller 之间来回传递动画状态,不如由两个 View Controller 引用的单独对象控制单个动画状态。

关于Swift - View Controller 之间向前和向后的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55173945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com