帮助我重新理解如何在(目标)C 中使用正弦来振荡图形

发布于 2024-10-08 04:54:08 字数 235 浏览 3 评论 0原文

我觉得问这个很傻,但我似乎忘记了如何有效地使用正弦。

我正在开发一个 iPad 应用程序,所以我在 ObjectiveC 中,我只是想让 UIView 缓慢振荡。仅使用position.y + sin(counter) 就会使它移动得如此之快以至于振动,而且我似乎无法定义减慢速度的周期。

我找到了一些代码示例(主要用于生成声波),它们都将如此多的东西组合到一行代码中,我无法轻易地将其拆开。有人可以解释一下我应该做什么吗?

I feel silly asking this but I seem to have forgotten how to use sine effectively.

I'm working on an iPad app so I'm in ObjectiveC, and I'm just trying to get a UIView to oscillate slowly. Just using position.y + sin(counter) makes it move so fast it vibrates and I can't seem to define the period to slow it down.

I've found some code samples (mostly for generating sound waves) and they all combine so many things into one line of code I can't easily pull it apart. Can anybody just explain what I should be doing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千纸鹤带着心事 2024-10-15 04:54:08

sin(和 cos)采用弧度参数。 counter 名称表明您没有使用弧度。

因此,您需要确定每秒需要多少次振荡。然后,您可以测量最后一个动画“帧”与当前动画“帧”之间的时间。整个波是 2PI 弧度,所以

Y = sin(2PI * Time * OccilationsPerSecond) * Amplitude. 

sin (and cos) take a parameter in Radians. The name counter suggests that you're not using Radians.

So, you need to determine how many oscillations per second you want. Then, you can measure the time between the last and the current animation 'frame'. A whole wave is 2PI radians, so

Y = sin(2PI * Time * OccilationsPerSecond) * Amplitude. 
夜空下最亮的亮点 2024-10-15 04:54:08

您需要这样的公式:

x(t) = (x_max) sin(2*pi*frequency*t)

其中

pi = 3.14159....
frequency = 1/period
t = time
x_max = maximum amplitude in the x-direction

如果您希望在 y 方向上振荡,则需要另一个函数。

You need a formula like this:

x(t) = (x_max) sin(2*pi*frequency*t)

where

pi = 3.14159....
frequency = 1/period
t = time
x_max = maximum amplitude in the x-direction

If you want oscillation in the y-direction you need another function.

后eg是否自 2024-10-15 04:54:08

虽然这不是您想要的答案,但如果计时器太快,您可以简单地减少调用计时器的时间。 (也就是说,您还可以设置 CABasicAnimation - 可能是一个更好的方法,因为 iOS 会为你处理动画,可以自动反转到初始设置,可以设置为循环“n”次,等等)

也就是说,希望其他人将提供您所追求的。

Whilst not the answer you're after, you could simply reduce the period you're calling the timer with if it's too fast. (That said, you can also set the duration for the CABasicAnimation - probably a much better approach, as iOS handles the animation for you, can auto-reverse to the initial settings, can be set to loop 'n' times, etc.)

That said, hopefully someone else will provide what you're after.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文