gpt4 book ai didi

ios - 在 UIView 动画中立即调用完成 block

转载 作者:行者123 更新时间:2023-11-28 21:55:48 25 4
gpt4 key购买 nike

我试图一个一个地改变 subview 的颜色,但是所有 View 的颜色都会立即改变,即使我已经将动画持续时间设置为 2 秒。

- (void)runAnimation
{
NSMutableArray *views = [[NSMutableArray alloc]init];

for (UIView *bubble in self.subviews)
{
if(bubble.tag == 2500)
[views addObject:bubble];
}

__weak PKCustomSlider *weak_self = self;
__block NSInteger currentView = 0;
self.animationBlock = ^{

[UIView animateWithDuration:2 animations:^{
NSLog(@"Animation block called again");
[views[currentView] setBackgroundColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:1]];
} completion:^(BOOL finished) {
currentView += 1;
if(!finished)
{
NSLog(@"Animation hasn't finished");
return;
}

if (currentView == views.count){
currentView = 0;
NSLog(@"Animation block ended");
}
else
{
weak_self.animationBlock();
NSLog(@"Animation block called again because views still available in the array");
}
}];

self.animationBlock();
}

最佳答案

如果您尝试为一个又一个 View 设置动画,则不必依赖完成 block 。如果动画 block 中没有要完成的动画,可以立即调用完成 block 。

你可以这样做:

- (void)runAnimation
{
for (int i = 0; i < self.subviews.count; i++) {
UIView *bubble = self.subviews[i];

if (bubble.tag != 2500)
continue;

[UIView animateWithDuration:2 delay:2*i options:0 animations:^{
[bubble setBackgroundColor:[UIColor colorWithRed:0 green:1 blue:0 alpha:1]];
} completion:nil];
}
}

关于ios - 在 UIView 动画中立即调用完成 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26634478/

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