霓虹灯(跑马灯、方框赛跑)

直接上代码:

     /*
        代码一: 霓虹灯(跑马灯)
     */

    NSArray *colorArray = [[[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor cyanColor], [UIColor blueColor], [UIColor purpleColor], nil] autorelease];
    CGFloat mulValue = [[UIScreen mainScreen] bounds].size.width / 7 ;
    for (int i = 0, j = 0; i < 7; i++,j++) {
        UIView *i = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width - (j * mulValue), [[UIScreen mainScreen] bounds].size.height - (j * mulValue))] ;
        i.backgroundColor = [colorArray objectAtIndex:j];
        i.tag = j + 1 ;
        i.center = self.window.center ;
        [self.window insertSubview:i atIndex:j] ;
        [i release];
    }
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(run) userInfo:nil repeats:YES] ;
    return YES;
}

- (void)run {
    UIColor *acolor = [self.window viewWithTag:7].backgroundColor;
    for (int i = 7; i >= 1; i--) {
        if (i > 1) {
            [self.window viewWithTag:i].backgroundColor = [self.window viewWithTag:(i - 1)].backgroundColor;
        }
        else{
            [self.window viewWithTag:i].backgroundColor = acolor;
        }
    }
}




NSArray *colors = @[[UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor cyanColor], [UIColor blueColor], [UIColor purpleColor]] ;

    for (int i = 0; i < colors.count; i++) {
        UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width - i * 60, self.window.bounds.size.height - i * 60)] ;
        aView.center = self.window.center ;
        aView.backgroundColor = colors[i] ;
        [self.window addSubview:aView] ;
        [aView release] ;
    }


     /*
         代码二:  霓虹灯(跑马灯)
     */

    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(_changBackgroundColor:) userInfo:[self.window subviews].lastObject repeats:YES] ;


    return YES;
}

- (void)_changBackgroundColor:(NSTimer *)sender {

    UIView *currentView = [sender userInfo] ;
    //暂存当前视图的颜色.
    UIColor *tempColor = currentView.backgroundColor ;
    //获取当前视图在子视图数组中的下标
    NSInteger currentIndex = [self.window.subviews indexOfObject:currentView] ;

    for (NSInteger i = currentIndex - 1; i >= 0; i--) {
        UIView *superView = self.window.subviews[i] ;
        currentView.backgroundColor = superView.backgroundColor ;
        currentView = superView ;
    }   
    currentView.backgroundColor = tempColor ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值