首页 > 其他 > 详细

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

时间:2015-07-14 11:47:34      阅读:89      评论:0      收藏:0      [点我收藏+]

直接上代码:

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

    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 ;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

原文:http://blog.csdn.net/zhengang007/article/details/46873081

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!