在 iOS 中使用动画更改标签背景颜色

Change label background color using animation in iOS

我正在尝试显示 n 个级别的动画。

UIStackView下有n个level。

我想要的: 使用动画

.......

问题是什么: 毫不拖延地立即更改所有背景颜色。

@property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *lblArray;



-(void)animationTestIntoLoop {

__block NSMutableArray* animationBlocks = [NSMutableArray new];
typedef void(^animationBlock)(BOOL);

// getNextAnimation
// removes the first block in the queue and returns it
animationBlock (^getNextAnimation)(void) = ^{

    if ([animationBlocks count] > 0){
        animationBlock block = (animationBlock)[animationBlocks objectAtIndex:0];
        [animationBlocks removeObjectAtIndex:0];
        return block;
    } else {
        return ^(BOOL finished){
            animationBlocks = nil;
        };
    }
};

for (UILabel *label in self.lblArray) {
    [animationBlocks addObject:^(BOOL finished){
        [UIView animateWithDuration:3 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
            //my first set of animations

            label.backgroundColor = [UIColor redColor];



        } completion: getNextAnimation()];
    }];

 }

getNextAnimation()(YES);

}

我在任何地方都找不到它的文档,但 UILabel 的 backgroundColor 属性 似乎不可设置动画。但是,只要您不设置标签视图本身的背景颜色,这个 hack 似乎就可以工作:

*#import <QuartzCore/QuartzCore.h>*
    
        
//Add this in ViewDidLoad

for (UILabel *label in self.lblArray)
{
    label.layer.backgroundColor = [UIColor whiteColor].CGColor;
}

-(void)animationTestIntoLoop
{
    __block NSMutableArray* animationBlocks = [NSMutableArray new];
    typedef void(^animationBlock)(BOOL);
    
    // getNextAnimation
    // removes the first block in the queue and returns it
    animationBlock (^getNextAnimation)(void) = ^{
        
        if ([animationBlocks count] > 0){
            animationBlock block = (animationBlock)[animationBlocks objectAtIndex:0];
            [animationBlocks removeObjectAtIndex:0];
            return block;
        } else {
            return ^(BOOL finished){
                animationBlocks = nil;
            };
        }
    };
    
    for (UILabel *label in self.lblArray)
    {
        [animationBlocks addObject:^(BOOL finished){
            
            [UIView animateWithDuration:2.0 animations:^{
                label.layer.backgroundColor = [UIColor redColor].CGColor;
            } completion:getNextAnimation()];
        }];
    }
    getNextAnimation()(YES);
}