全局变量{
NSArray *list;
int a;
NSTimer *timer;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window makeKeyAndVisible];
#pragma mark------3-------
#pragma mark-----跑马灯-------
a = 1;//给tag赋初始值
list = [NSArray array];//初始化数组
list = @[[UIColor purpleColor],[UIColor redColor],[UIColor blueColor],[UIColor greenColor],[UIColor lightGrayColor]];
for (int i=0; i<list.count; i++) {
view = [[UIView alloc]initWithFrame:CGRectMake(100*i, 150, 50, 50)];//创建视图
view.backgroundColor = list[arc4random()%list.count];
view.tag = i+1;
view.alpha = 0;
[self.window addSubview:view];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"开始" forState:UIControlStateNormal];
[button1 setTitle:@"暂停" forState:UIControlStateNormal];
button.backgroundColor = [UIColor purpleColor];
button1.backgroundColor = [UIColor blueColor];
button.frame = CGRectMake(80, 300, 50, 50);
button1.frame = CGRectMake(300 , 300, 50, 50);
[self.window addSubview:button1];
[self.window addSubview:button];
[button addTarget:self action:@selector(kaishi) forControlEvents:UIControlEventTouchDown];
[button1 addTarget:self action:@selector(over) forControlEvents:UIControlEventTouchDown];
return YES;
}
-(void)kaishi{
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(start) userInfo:nil repeats:YES];
}
-(void)over{
[timer invalidate];
}
-(void)start{
switch (a) {
case 1:
[self.window viewWithTag:a].backgroundColor = list[1];
[self.window viewWithTag:a].alpha = 1;
a++;
break;
case 2:
[self.window viewWithTag:a-1].alpha = 0;
[self.window viewWithTag:a].backgroundColor = list[2];
[self.window viewWithTag:a].alpha = 1;
a++;
break;
case 3:
[self.window viewWithTag:a-1].alpha = 0;
[self.window viewWithTag:a].backgroundColor = list[4];
[self.window viewWithTag:a].alpha = 1;
a++;
break;
case 4:
[self.window viewWithTag:a-1].alpha = 0;
[self.window viewWithTag:a].backgroundColor = list[0];
[self.window viewWithTag:a].alpha = 1;
a++;
break;
default:
a = 1;
[self.window viewWithTag:4].alpha = 0;
break;
}
}