每次动画重复时调用一个方法(Call a method every time animation repeats)

编程入门 行业动态 更新时间:2024-10-16 16:29:45
每次动画重复时调用一个方法(Call a method every time animation repeats)

我需要在动画重复之前调用某个方法。 我不知道在哪里放置那行代码。

这是我为对象设置动画的代码:

[UIView animateWithDuration:speed delay:delay options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveLinear|UIViewAnimationOptionRepeat animations:^(void){ cloud.frame = (CGRectMake(cloud.frame.origin.x, cloud.frame.origin.y+600, cloud.frame.size.width, cloud.frame.size.height)); }completion:^(BOOL finished){ if (finished){ [cloud setBackgroundImage:[UIImage imageNamed:@"cloudWhite.png"] forState:UIControlStateNormal]; } }];

完成部分中调用的方法将不会在每次重复之前执行,而是仅在整个动画完成之后执行,并且在重复之后 - 在这种情况下 - 从不执行。

我的问题是,如何在每个动画重复之前调用一个方法?

I need to call a certain method before my animation repeats. I don't know where to place that line of code.

This is my code for animating an object:

[UIView animateWithDuration:speed delay:delay options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveLinear|UIViewAnimationOptionRepeat animations:^(void){ cloud.frame = (CGRectMake(cloud.frame.origin.x, cloud.frame.origin.y+600, cloud.frame.size.width, cloud.frame.size.height)); }completion:^(BOOL finished){ if (finished){ [cloud setBackgroundImage:[UIImage imageNamed:@"cloudWhite.png"] forState:UIControlStateNormal]; } }];

Method called in completion part will not be executed before every repeat but only after whole animation is complete, together with al repeats - which is, in this case - never.

My question is, how to call a method before every animation repeat?

最满意答案

我建议重构你的动画,这样它就不会使用UIViewAnimationOptionRepeat 。 相反,编写一个执行动画的单个循环的方法。 然后添加一个以递归方式调用该方法的完成方法块。

您还可以在完成块中的动画的每次迭代中放置要触发的方法调用。

I would suggest refactoring your animation so it does not use UIViewAnimationOptionRepeat. Instead, write a method that executes a single cycle of your animation. Then add a completion method block that calls the method recursively.

You can also put the method call that you want to fire on each iteration of your animation in the completion block.

更多推荐

本文发布于:2023-07-16 12:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128645.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时调   方法   动画   Call   repeats

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!