1

我尝试从 animateWithDuration 创建方法并返回 BOOL 类型。但是在完成块上似乎没有检测到我的对象。有人可以向我解释一下,为什么会发生这种情况?

+ (BOOL)showAnimationFirstContent:(UIView *)view {
    BOOL status = NO;

    CGRect show = [SwFirstContent rectFirstContentShow];

    [UIView animateWithDuration:DURATION
                          delay:DELAY
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{ view.frame = show; }
                     completion:^( BOOL finished ) {
                         status = YES;
                     }];
    return status;
}

提前谢谢。

4

2 回答 2

3

您正在将异步执行的块内设置状态值。这意味着,您的 return 语句不能保证在块执行后执行。要知道您的动画何时完成,您需要以不同的方式声明您的方法。

+ (void)showAnimationFirstContent:(UIView *)view completion:(void (^)(void))callbackBlock{

    CGRect show = [SwFirstContent rectFirstContentShow];

    [UIView animateWithDuration:DURATION
                          delay:DELAY
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{ view.frame = show; }
                     completion:^( BOOL finished ) {
                         callbackBlock();
                     }];
}

你可以这样调用这个方法:

[MyClass showAnimationFirstContent:aView completion:^{
//this block will be executed when the animation will be finished
    [self doWhatEverYouWant];
}];

您可能想了解更多有关block 工作原理的信息。

希望这可以帮助。

于 2013-08-26T07:37:36.707 回答
2

这是因为块是异步执行的。这意味着在执行该animateWithDuration方法后,该showAnimationFirstContent方法将继续执行(在这种情况下返回),而无需等待动画完成(并将布尔值更改为YES)。

您可能应该将此布尔值保留为动画类的成员,并在完成块中执行一个方法以在动画完成时处理此布尔值

于 2013-08-26T07:27:59.400 回答