UIView iPhone SDK嵌套动画

UIView iPhone SDK嵌套动画,iphone,uiview,sdk,animation,Iphone,Uiview,Sdk,Animation,我想为我的视图创建一个嵌套动画 我有一个动画停止选择器,它被称为刚刚好: [UIView setAnimationDidStopSelector:@selector(growAnimationDidStop1:finished:context:)]; 然而,在这个选择器中,我想做更多的动画,完成后,将调用另一个选择器: - (void)growAnimationDidStop1:(NSString *)animationID finished:(NSNumber *)finished cont

我想为我的视图创建一个嵌套动画

我有一个动画停止选择器,它被称为刚刚好:

[UIView setAnimationDidStopSelector:@selector(growAnimationDidStop1:finished:context:)];
然而,在这个选择器中,我想做更多的动画,完成后,将调用另一个选择器:

- (void)growAnimationDidStop1:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{
...
    [UIView setAnimationDidStopSelector:@selector(growAnimationDidStop2:finished:context:)];
... 

    [UIView commitAnimations];
}

问题是从未调用
growtanimationdidstop2
。为什么会这样?

哎呀,我自己回答了。我必须用第一个停止方法开始一个全新的动画环境,我自己回答说。我必须在第一个停止方法中启动一个全新的动画上下文

你也可以使用
,苹果现在推荐这样做。将下面的代码视为伪代码:

[UIView animateWithDuration:0.3
                        delay:0.0
                      options:UIViewAnimationCurveEaseInOut
                   animations:^{
                     [self doAnimationOne];
                   } completion:^(BOOL finished){
                     [UIView animateWithDuration:0.4
                                           delay:0.0
                                         options:UIViewAnimationCurveEaseInOut
                                      animations:^{
                                        [self doAnimationNine];
                                      }
                                      completion:^(BOOL finished) {
                                        [UIView animateWithDuration:0.3
                                                              delay:0.0
                                                            options:UIViewAnimationCurveEaseInOut
                                                         animations:^{
                                                           [self doAnimationFive];
                                                         } 
                                                         completion:^(BOOL finished) {}];
                                      }];
                   }];
动画“社交化”也是一种很好的做法,例如:

BOOL userInteractionEnabled = [self isUserInteractionEnabled];
[self setUserInteractionEnabled:NO];

BOOL animationsEnabled = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:YES];
在开始动画之前,然后在最终完成块中执行以下操作:

[UIView setAnimationsEnabled:animationsEnabled];
[self setUserInteractionEnabled:userInteractionEnabled];

你也可以用
块来实现这一点,苹果现在推荐这样做。将下面的代码视为伪代码:

[UIView animateWithDuration:0.3
                        delay:0.0
                      options:UIViewAnimationCurveEaseInOut
                   animations:^{
                     [self doAnimationOne];
                   } completion:^(BOOL finished){
                     [UIView animateWithDuration:0.4
                                           delay:0.0
                                         options:UIViewAnimationCurveEaseInOut
                                      animations:^{
                                        [self doAnimationNine];
                                      }
                                      completion:^(BOOL finished) {
                                        [UIView animateWithDuration:0.3
                                                              delay:0.0
                                                            options:UIViewAnimationCurveEaseInOut
                                                         animations:^{
                                                           [self doAnimationFive];
                                                         } 
                                                         completion:^(BOOL finished) {}];
                                      }];
                   }];
动画“社交化”也是一种很好的做法,例如:

BOOL userInteractionEnabled = [self isUserInteractionEnabled];
[self setUserInteractionEnabled:NO];

BOOL animationsEnabled = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:YES];
在开始动画之前,然后在最终完成块中执行以下操作:

[UIView setAnimationsEnabled:animationsEnabled];
[self setUserInteractionEnabled:userInteractionEnabled];

按顺序执行多个动画的最佳方法是使用块队列

查看结果的干净程度:

NSMutableArray* animationBlocks = [NSMutableArray new];

typedef void(^animationBlock)(BOOL);

// getNextAnimation
// removes the first block in the queue and returns it
animationBlock (^getNextAnimation)() = ^{
    animationBlock block = (animationBlock)[animationBlocks firstObject];
    if (block){
        [animationBlocks removeObjectAtIndex:0];
        return block;
    }else{
         return ^(BOOL finished){};
    }
};

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    [UIView animateWithDuration:1.0 animations:^{
        //...animation code...
    } completion: getNextAnimation()];
}];

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    [UIView animateWithDuration:1.0 animations:^{
        //...animation code...
    } completion: getNextAnimation()];
}];

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    NSLog(@"Multi-step Animation Complete!");
}];

// execute the first block in the queue
getNextAnimation()(YES);

摘自:

按顺序制作多个动画的最佳方法是使用块队列

查看结果的干净程度:

NSMutableArray* animationBlocks = [NSMutableArray new];

typedef void(^animationBlock)(BOOL);

// getNextAnimation
// removes the first block in the queue and returns it
animationBlock (^getNextAnimation)() = ^{
    animationBlock block = (animationBlock)[animationBlocks firstObject];
    if (block){
        [animationBlocks removeObjectAtIndex:0];
        return block;
    }else{
         return ^(BOOL finished){};
    }
};

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    [UIView animateWithDuration:1.0 animations:^{
        //...animation code...
    } completion: getNextAnimation()];
}];

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    [UIView animateWithDuration:1.0 animations:^{
        //...animation code...
    } completion: getNextAnimation()];
}];

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    NSLog(@"Multi-step Animation Complete!");
}];

// execute the first block in the queue
getNextAnimation()(YES);

摘自:

只是想添加一条评论。[UIView animateWithDuration]在动画期间自动禁用用户交互。为了在动画期间启用交互,您必须将UIViewAnimationOptionAllowUserInteraction作为动画选项传递。只想添加注释。[UIView animateWithDuration]在动画期间自动禁用用户交互。为了在动画期间启用交互,必须将UIViewAnimationOptionAllowUserInteraction作为动画选项传递。