gpt4 book ai didi

swift - 尝试快速移动 x 和 y 字段中的图像

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

我正在尝试制作从左到右在屏幕上移动的波浪的动画,同时我希望它稍微上下移动。代码如下:

UIView.animateWithDuration(2.5, delay: 0,
options: .Repeat | .Autoreverse, animations: {
self.wavesImg.center.y += 50
self.wavesImg.center.x -= self.wavesImg.image!.size.width/7
}, completion: nil)

目前,它只执行最后一个动画,因此它只从左到右移动。我怎样才能让它上下移动?

最佳答案

为什么不直接对 y 应用 sin 来赋予它自然的波浪感

UIView.animateWithDuration(2.5, delay: 0,
options: .Repeat | .Autoreverse, animations: {
self.wavesImg.center.y += 50 * sin(self.wavesImg.center.x)
self.wavesImg.center.x -= self.wavesImg.image!.size.width/7
}, completion: nil)

我不确定你想如何处理 sin 中的值,现在我将每个 x 值视为 1 弧度,因为 sin 函数采用弧度,但你可能想改变它以适合你的需要。 (也就是说,如果您希望波从 0 开始,然后在 x 为 10 时上升并返回到 0,您可以执行类似 sin((x/10) * PI) 的操作

关于swift - 尝试快速移动 x 和 y 字段中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33178518/

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