iOS-多个动画块? [英] iOS - multiple animation blocks?
问题描述
我有多个动画必须连锁运行.我一直在处理的方法是使用completionHandler并运行下一个动画块.有没有更清洁的方法来解决这个问题?
I have multiple animations that have to run as a chain. The way I've been dealing with this is to use a completionHandler and run the next animation block. Is there a cleaner way to deal with this?
[UIView animateWithDuration:1 animations^{
// perform first animation
}completion:(BOOL finished){
[UIView animateWithDuration:1 animations^{
// perform second animation
}completion:(BOOL finished){
}];
}];
推荐答案
您还可以使用 animateWithDuration:delay:options:animations:completion:
并具有交错的延迟,以便按顺序开始,但是通常最好用完成块来完成它们.
You can also use animateWithDuration:delay:options:animations:completion:
with staggered delays so that they start in order, but typically it's best to do them with completion blocks.
如果有多个代码并且使代码难以阅读,则只需分解出块即可(我是在头顶上键入此代码,因此可能无法编译):
If there are several and it's making the code difficult to read, just factor out the blocks (I'm typing this off the top of my head, so it may not compile):
typedef void (^AnimationBlock)();
AnimationBlock firstAnimation = ^{ ... };
AnimationBlock secondAnimation = ^{ ... };
[UIView animateWithDuration:1 animations:firstAnimation completion:(BOOL finished) {
[UIView animateWithDuration:1 animations:secondAnimation];}];
您可以在 UIView
上创建一个类别,该类别采用了一系列此类块并将它们链接在一起,但是您必须处理所有极端情况.您如何定义时间?您如何处理中止的动画等.以上可能是大多数情况下的最佳方法.
You could create a category on UIView
that took an array of such blocks and chained them together for you, but then you'd have to deal with all the corner cases. How do you define the timings; how do you deal with aborted animations, etc. The above is probably the best approach in most cases.
这篇关于iOS-多个动画块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!