帮助我重新理解如何在(目标)C 中使用正弦来振荡图形
我觉得问这个很傻,但我似乎忘记了如何有效地使用正弦。
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
sin(和 cos)采用弧度参数。
counter
名称表明您没有使用弧度。因此,您需要确定每秒需要多少次振荡。然后,您可以测量最后一个动画“帧”与当前动画“帧”之间的时间。整个波是 2PI 弧度,所以
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 方向上振荡,则需要另一个函数。
You need a formula like this:
where
If you want oscillation in the y-direction you need another function.
虽然这不是您想要的答案,但如果计时器太快,您可以简单地减少调用计时器的时间。 (也就是说,您还可以设置 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.