gpt4 book ai didi

ios - 这个 block 队列中发生了什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:38 24 4
gpt4 key购买 nike

我正在寻找一种对动画 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);

最佳答案

首先感谢分享这篇文章,这是个好主意。这非常适合我:

@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);

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

希望对您有所帮助。

关于ios - 这个 block 队列中发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21874664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com