Чтобы сделать что-то подобное вручную, вам нужно будет запустить новую анимацию по окончании текущей.Есть несколько способов сделать это, но один из них - упаковать все данные, необходимые для вашей анимации, в класс, а затем создать массив таких классов.Затем вы можете создать один метод, который вызывается рекурсивно.Примерно так:
- (void)performAllAnimations:(NSArray *)allAnimations
{
NSMutableArray *duplicate = [allAnimations mutableCopy];
AnimationDescription *currentAnimation = duplicate.firstObject;
[duplicate removeObjectAtIndex:0];
[UIView animateWithDuration:currentAnimation.duration animations:^{
// Use currentAnimation to do the animation
} completion:^(BOOL finished) {
if(duplicate.count > 0) [self performAllAnimations:duplicate];
}];
}
Вам не всегда нужен AnimationDescription
в качестве пользовательского класса.В вашем случае может быть достаточно просто использовать массив индексов.Например, вы можете сделать:
- (void)performAllAnimations:(NSArray *)allAnimations
{
NSMutableArray *duplicate = [allAnimations mutableCopy];
int currentAnimationIndex = [duplicate.firstObject integerValue];
[duplicate removeObjectAtIndex:0];
[UIView animateWithDuration:0.3 animations:^{
// Use currentAnimation to do the animation
} completion:^(BOOL finished) {
if(duplicate.count > 0) [self performAllAnimations:duplicate];
}];
}
Затем вы можете сделать что-то вроде:
NSMutableArray *indexArray = [[NSMutableArray alloc] init];
for(int i=0; i<10; i++) {
[indexArray addObject:@(i)];
}
[self performAllAnimations: indexArray];
Естественно используя свои собственные данные.
Другой подход заключается в использовании задержки,В некоторых случаях это может быть лучше, поскольку у вас больше контроля над началом следующей анимации.Смотрите следующий пример:
- (void)performAllAnimations:(NSArray *)allAnimations withAnimationExecution:(void (^)(id animationObject, int index))execution
{
NSTimeInterval duration = 0.3;
for(int i=0; i<allAnimations.count; i++) {
[UIView animateWithDuration:duration delay:(duration*0.5)*i options:UIViewAnimationOptionCurveEaseInOut animations:^{
execution(allAnimations[i], i);
} completion:^(BOOL finished) {
}];
}
}
- (void)testAnimation
{
NSMutableArray *views = [[NSMutableArray alloc] init];
for(int i=0; i<10; i++) {
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 50.0, 100.0, 100.0)];
[self.view addSubview:aView];
aView.backgroundColor = [UIColor blackColor];
aView.layer.borderWidth = 2.0;
aView.layer.borderColor = [UIColor yellowColor].CGColor;
[views addObject:aView];
}
[self performAllAnimations:views withAnimationExecution:^(id animationObject, int index) {
UIView *view = animationObject;
view.frame = CGRectMake(75.0*index, 100.0, 100.0, 100.0);
}];
}