繁体   English   中英

iOS中的连续闪烁动画

[英]sequential blinking light animation in iOS

想象一下一台老式的弹球机(或类似产品),该机器的顶部记分牌面板的两侧都有三个灯泡。 灯泡编号为1-6

我希望能够调用一种方法,该方法将使灯闪烁0.5秒,然后再次关闭,并传递数字数组作为灯闪烁的顺序

我写了下面的代码,它确实有效,但看起来效率很低,也很冗长。 我认为使用计时器会更好,但我不确定如何使用我的“序列”数组

有一个更好的方法吗?

- (IBAction)testLights:(id)sender {

    NSArray *sequence = [[NSArray alloc] initWithObjects: [NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4],[NSNumber numberWithInt:5],nil];
    [self setLights:sequence];
}

- (void)setLights:(NSArray *)sequence {

    UIImageView *light=[lgtArray objectAtIndex: [[sequence objectAtIndex:0]integerValue]];
    light.alpha = 0;
    [UIView animateWithDuration:0.3f
                     animations:^{
                         light.alpha = 1;
                     }completion:^(BOOL finished){

                         //new annimation
                         light.alpha = 0;
                         UIImageView *light=[lgtArray objectAtIndex: [[sequence objectAtIndex:1]integerValue]];
                         [UIView animateWithDuration:0.3f
                                          animations:^{
                                              light.alpha = 1;
                                          }completion:^(BOOL finished){

                                              //new annimation
                                              light.alpha = 0;
                                              UIImageView *light=[lgtArray objectAtIndex: [[sequence objectAtIndex:2]integerValue]];
                                              [UIView animateWithDuration:0.3f
                                                               animations:^{
                                                                   light.alpha = 1;
                                                               }completion:^(BOOL finished){

                                                                   //new annimation
                                                                   light.alpha = 0;
                                                                   UIImageView *light=[lgtArray objectAtIndex: [[sequence objectAtIndex:3]integerValue]];
                                                                   [UIView animateWithDuration:0.3f
                                                                                    animations:^{
                                                                                        light.alpha = 1;
                                                                                    }completion:^(BOOL finished){

                                                                                        //new annimation
                                                                                        light.alpha = 0;
                                                                                        UIImageView *light=[lgtArray objectAtIndex: [[sequence objectAtIndex:4]integerValue]];
                                                                                        [UIView animateWithDuration:0.3f
                                                                                                         animations:^{
                                                                                                             light.alpha = 1;
                                                                                                         }completion:^(BOOL finished){

                                                                                                             //new annimation
                                                                                                             light.alpha = 0;
                                                                                                             UIImageView *light=[lgtArray objectAtIndex: [[sequence objectAtIndex:5]integerValue]];
                                                                                                             [UIView animateWithDuration:0.3f
                                                                                                                              animations:^{
                                                                                                                                  light.alpha = 1;
                                                                                                                              }completion:^(BOOL finished){

                                                                                                                                  light.alpha = 0;


                                                                                                                              }];


                                                                                                         }];


                                                                                    }];


                                                               }];



                                          }];


                     }];


}

这可以通过使函数递归来实现。 我建议您将序列对象作为NSMutableArray传递。

- (void)setLights:(NSMutableArray *)sequence {

    if ([sequence count] > 0)
    {
        UIImageView *light=[lgtArray objectAtIndex: [[sequence objectAtIndex:0]integerValue]];
        [sequence removeObjectAtIndex:0];
        light.alpha = 0;
        [UIView animateWithDuration:0.3f
                         animations:^{
                             light.alpha = 1;
                         }completion:^(BOOL finished){

                             [self setLights:sequence];
                         }];
    }
}

此功能的另一个优点是能够处理任意数量的序列,而不仅仅是6。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM