需求:
设置一个颜色切换的彩灯,可以正向/逆向以及停止.
功能如下图:
设计思路:
(1)用一个数组装载颜色
(2)用同样长度的数组装载图片
(3)使用定时器进行流水切换
切换:使用%进行上一中颜色和上一张图片切换.
代码:
// // ViewController.h // ColorLed // DRAGON // Created by 5016 on 13-12-25. // Copyright (c) 2013年 dradon. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController { UIButton *btn; NSMutableArray *btnArr; UIView *view; NSMutableArray *viewArr; NSMutableArray *bottomviewArr; //颜色数组 NSMutableArray *colorArr; NSTimer *sTimer; NSTimer *rTimer; } @end
// // ViewController.m // ColorLed // // Created by 5016 on 13-12-25. // Copyright (c) 2013年 dradon. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; btnArr = [[NSMutableArray alloc]init]; viewArr = [[NSMutableArray alloc]init]; bottomviewArr = [[NSMutableArray alloc]init]; colorArr = [[NSMutableArray alloc]init]; [self initColor]; [self initControl]; } //初始化颜色 -(void)initColor { [colorArr addObject:[UIColor blackColor]]; [colorArr addObject:[UIColor darkGrayColor]]; [colorArr addObject:[UIColor lightGrayColor]]; [colorArr addObject:[UIColor whiteColor]]; [colorArr addObject:[UIColor grayColor]]; [colorArr addObject:[UIColor redColor]]; [colorArr addObject:[UIColor greenColor]]; [colorArr addObject:[UIColor blueColor]]; [colorArr addObject:[UIColor cyanColor]]; [colorArr addObject:[UIColor yellowColor]]; [colorArr addObject:[UIColor magentaColor]]; [colorArr addObject:[UIColor orangeColor]]; [colorArr addObject:[UIColor purpleColor]]; [colorArr addObject:[UIColor brownColor]]; [colorArr addObject:[UIColor clearColor]]; } //初始化控件 -(void)initControl { [self initView]; [self initBtn]; } //初始化按键 -(void)initBtn { int tagNum = 101; for (int i = 0; i<3; i++) { btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btn setFrame:CGRectMake(12+100*i,400,90,50)]; [btn setTag:tagNum++]; [btnArr addObject:btn]; //绑定事件 [btn addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:btn]; [btn release]; } [((UIButton*)[btnArr objectAtIndex:0]) setTitle:@"正向" forState:UIControlStateNormal]; [((UIButton*)[btnArr objectAtIndex:1]) setTitle:@"反向" forState:UIControlStateNormal]; [((UIButton*)[btnArr objectAtIndex:2]) setTitle:@"停止" forState:UIControlStateNormal]; } //按键响应事件 -(void)onclick:(UIButton*)button { NSLog(@"你点击了"); switch (button.tag) { case 101: [self start]; break; case 102: [self reverst]; break; case 103: [self stop]; break; } } //初始化UIView -(void)initView { [self initTopView]; [self initBottomView]; } //上面图 -(void)initTopView { int scale = 12; for (int i = 0; i<[colorArr count]; i++) { view = [[UIView alloc] initWithFrame:CGRectMake(10+scale*i, 10+scale*i, 300-2*scale*i, 300-2*scale*i)]; [view setBackgroundColor:((UIColor*)[colorArr objectAtIndex:i])]; [viewArr addObject:view]; [self.view addSubview:view]; [view release]; } } //下面图 -(void)initBottomView { int scale = 22; for (int i = 0; i<[colorArr count]; i++) { view = [[UIView alloc] initWithFrame:CGRectMake(8+scale*i, 320, 22, 70)]; [view setBackgroundColor:((UIColor*)[colorArr objectAtIndex:i])]; [bottomviewArr addObject:view]; [self.view addSubview:view]; [view release]; } } /**************************/ static float startNum = 1; -(void)start { retNum=1; [sTimer invalidate]; [rTimer invalidate]; //1.开始定时器 sTimer = [[NSTimer scheduledTimerWithTimeInterval:startNum target:self selector:@selector(startAction:) userInfo:nil repeats:YES] retain]; startNum -=0.1; } static int num = 0; -(void)startAction:(NSTimer*)timer { int Mod = [colorArr count]; for (int i = 0; i < Mod - 2 ; i++) { [(UIView*)[viewArr objectAtIndex:i] setBackgroundColor:((UIColor*)[colorArr objectAtIndex:(++num)%Mod])]; [(UIView*)[bottomviewArr objectAtIndex:i] setBackgroundColor:((UIColor*)[colorArr objectAtIndex:(++num)%Mod])]; } } static float retNum = 1; -(void)reverst { startNum = 1; [rTimer invalidate]; [sTimer invalidate]; rTimer = [[NSTimer scheduledTimerWithTimeInterval:retNum target:self selector:@selector(startAction1:) userInfo:nil repeats:YES] retain]; retNum -=0.1; } -(void)startAction1:(NSTimer*)timer { int Mod = [colorArr count]; for (int i = Mod - 2 ;i >0 ; i--) { [(UIView*)[viewArr objectAtIndex:i] setBackgroundColor:((UIColor*)[colorArr objectAtIndex:(++num)%Mod])]; [(UIView*)[bottomviewArr objectAtIndex:i] setBackgroundColor:((UIColor*)[colorArr objectAtIndex:(++num)%Mod])]; } } //停止 -(void)stop { NSLog(@"停止"); [sTimer invalidate]; [rTimer invalidate]; } /**************************/ - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end