码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 霓虹灯简单小程序

时间:2014-05-07 19:30:16      阅读:414      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

在RootViewController.m文件中

- (void)viewDidLoad//视图加载方法

bubuko.com,布布扣
- (void)viewDidLoad
{
    
    //设置红色
    UIView *viewRed = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 120, 30)];
    viewRed.backgroundColor = [UIColor redColor];
    [self.view addSubview:viewRed];
    
    //设置橙色
    UIView *viewOrange = [[UIView alloc]initWithFrame:CGRectMake(100, 130, 120, 30)];
    viewOrange.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:viewOrange];
    
    //设置黄色
   
    UIView *viewYellow = [[UIView alloc]initWithFrame:CGRectMake(100, 160, 120, 30)];
    viewYellow.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:viewYellow];
    
    //设置绿色
    UIView *viewGreen = [[UIView alloc]initWithFrame:CGRectMake(100, 190, 120, 30)];
    viewGreen.backgroundColor = [UIColor greenColor];
    [self.view addSubview:viewGreen];
    
    //设置青色
    UIView *viewGray = [[UIView alloc]initWithFrame:CGRectMake(100, 220, 120, 30)];
    viewGray.backgroundColor = [UIColor grayColor];
    [self.view addSubview:viewGray];
    
    //设置蓝色
    UIView *viewBlue = [[UIView alloc]initWithFrame:CGRectMake(100, 250, 120, 30)];
    viewBlue.backgroundColor = [UIColor blueColor];
    [self.view addSubview:viewBlue];

    //设置紫色
    UIView *viewPurple = [[UIView alloc]initWithFrame:CGRectMake(100, 280, 120, 30)];
    viewPurple.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:viewPurple];
    
    //设置按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(100, 330, 120, 30);
    [button setTitle:@"开始" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor blackColor];
    button.tag = 108;
    [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
bubuko.com,布布扣

在关联方法btnAction中:

 timer为全局变量

 可以在RootViewController.h文件中定义;

bubuko.com,布布扣
-(void)btnAction:(id)sender
{
    UIButton *button = (UIButton*)sender;
    if (button.tag ==108) {
        timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(doChangeColor:) userInfo:nil repeats:YES];
        [timer fire];//计时器开始
    }
    
}
bubuko.com,布布扣

在关联方法doChangeColor中:

bubuko.com,布布扣
-(void)doChangeColor:(id)sender
{
    NSArray *arr = self.view.subviews;//得到所有的视图
    int count = [arr count]-1;//得到计数
    UIView *firstView = [arr objectAtIndex:0];
    UIColor *firstColor = firstView.backgroundColor;
    
    for (int i=0; i<count; i++) {
        UIView *view = [arr objectAtIndex:i];
        UIView *nextView = [arr objectAtIndex:(i+1)];
        view.backgroundColor = nextView.backgroundColor;
    }
    ((UIView*)[arr objectAtIndex:count]).backgroundColor = firstColor;
    self.view.backgroundColor = [UIColor colorWithRed:(arc4random()%255/255.0) green:(arc4random()%255/255.0) blue:(arc4random()%255/255.0) alpha:(arc4random()%255/255.0)];
    
}
bubuko.com,布布扣

结果显示:

 bubuko.com,布布扣

 会不断的改变背景图片,和实现了霓虹灯的效果。

IOS 霓虹灯简单小程序,布布扣,bubuko.com

IOS 霓虹灯简单小程序

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/taopengcun/p/3709728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!