这个块队列中发生了什么?

时间:2014-02-19 08:17:10

标签: ios iphone objective-c block

我正在寻找一种排队动画块的方法,并发生在这篇博客文章中: http://xibxor.com/2013/03/27/uiview-animation-without-nested-hell/

我无法让它发挥作用,但......如何安排这些元素的范围尚不清楚。另外,第18,25和32行的分号是什么?任何人都可以解释如何使用它吗?

编辑:这是从源代码复制的代码:

NSMutableArray* animationBlocks = [NSMutableArray new];

typedef void(^animationBlock)(BOOL);

// getNextAnimation
// removes the first block in the queue and returns it
animationBlock (^getNextAnimation)() = ^{
    animationBlock block = animationBlocks.count ? (animationBlock)[animationBlocks objectAtIndex:0] : nil;
    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){;
    [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);

1 个答案:

答案 0 :(得分:2)

首先感谢分享这篇文章,这是一个好主意。这对我很有用:

@interface之外的.h中

typedef void(^animationBlock)(BOOL);

然后在你的.m(这里是- (void)viewDidLoad内):

NSMutableArray* animationBlocks = [NSMutableArray new];

UILabel* labelTest = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
labelTest.text = @"Label Test";

[self.view addSubview:labelTest];

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

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    [UIView animateWithDuration:2.0 animations:^{
        NSLog(@"1-step Animation Complete!");
        labelTest.alpha = 0;
    } completion: getNextAnimation()];
}];

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    [UIView animateWithDuration:2.0 animations:^{
        labelTest.text = @"New text";
    } completion: getNextAnimation()];
}];

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    [UIView animateWithDuration:2.0 animations:^{
        labelTest.alpha = 1;
    } 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);

这是一个非常简单的动画但你需要它来测试它^^

希望这会有所帮助。