如何循环动画并在每次迭代时进行一些更改?
我希望我的动画循环播放并逐渐减慢速度。我认为在这种情况下不可能使用“重复”属性,因为动画必须在每次迭代结束时减慢速度。我的计划很简单:
1)创建并初始化一个动画(在 for 循环体之外)。
2)创建一个for循环。在此循环的主体中设置动画的持续时间属性。并开始动画将其添加到我的视图中(它是显式动画)。
3) 等待持续时间并继续执行循环。
我使用 usleep() 函数等待动画完成。在下一个循环迭代中,添加相同的动画,并且更改其持续时间属性(增加以减慢动画速度)。这是代码(初始化动画的所有代码都在上面,无需进行以下修改即可正常工作):
for (int i=1; i<10; i++) {
// Find the duration (I simplified this part it depends on i value).
keyframeAnim.duration = ...;
// Add an animation.
[self addAnimation:keyframeAnim forKey:@"loopingLongAnimation"];
// Wait till it stops.
usleep(keyframeAnim.duration);
}
问题是这无法正常工作。它只播放示例动画一次并停止。看来我错过了核心动画中非常重要的一些东西。为此目的是否有一些更合适或合适的解决方案?我可以使用计时曲线来影响整个循环动画而不仅仅是它的示例部分吗?
提前致谢。
I want my animation to loop and slow down gradually. I dont think that it is possible to use "repeats" property in this case because animation has to slow down at the end of each iteration. My plan was simple:
1) Create and initialize an animation (outside the for-loop body).
2) Create a for loop. In the body of this loop set the duration property for my animation. And start animation adding it to my view (it is explicit animation).
3) Wait for the duration time and proceed executing the loop.
I used usleep() function to wait till the animation finishes. On the next loop iteration the same animation is added and it's duration property is changed (increased to slow down the animation). Here's the code (all the code to initialize animation is above and it works fine without the following modifications):
for (int i=1; i<10; i++) {
// Find the duration (I simplified this part it depends on i value).
keyframeAnim.duration = ...;
// Add an animation.
[self addAnimation:keyframeAnim forKey:@"loopingLongAnimation"];
// Wait till it stops.
usleep(keyframeAnim.duration);
}
The problem is this doesn't work properly. It only plays sample animation once and stops. Looks like I missed somthing very essential to the Core Animation. Are there some more appropriate or suitable solutions for this purpose? Can i use timing curve to affect the whole looping animation not only the it's sample part?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有渲染任何内容,因为渲染需要泵送运行循环。请参阅我对循环显示图像
我认为在这种情况下,您也许可以为每个动画设置一个延迟,相当于前一个动画的延迟+持续时间,因此动画在时间轴上从头到尾对齐。
但最好只是连接一个动画委托来响应动画并通过启动下一个较慢的动画来停止消息。
这两者都使你的动画循环异步运行,以便运行循环可以正常执行。
Nothing is rendered, because rendering necessitates pumping the run loop. See my answer to Displaying images in sequence with a loop
I think in this case you could perhaps set a delay on each animation, equivalent to the previous animation's delay + duration, so the animations are aligned head-to-tail on the timeline.
But it would probably be better to just wire up an animation delegate that responds to the animation did stop message by starting the next, slower animation.
Both of these make your animation loop run asynchronously, so that the run loop can execute normally.