首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在这个块队列中发生了什么?

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

Stack Overflow用户
提问于 2014-02-19 08:17:10
回答 1查看 161关注 0票数 2

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

我无法让它工作,如何安排这些元素的though...the范围还不清楚。另外,第18、25和32行中的分号是做什么的?有人能解释一下怎么用这个吗?

编辑:下面是从源代码复制的代码:

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-19 08:33:58

首先,感谢分享这篇文章,这是一个好主意。这对我来说非常有效:

在你的.h外的@interface

代码语言:javascript
复制
typedef void(^animationBlock)(BOOL);

然后在您的.m中(这里是在- (void)viewDidLoad中):

代码语言:javascript
复制
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);

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

希望这会有所帮助。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21874664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档