//先创建一个霓虹灯视图
NSArray *arr = [NSArray arrayWithObjects:[UIColor purpleColor],[UIColor cyanColor],[UIColor blueColor],[UIColor greenColor],[UIColor yellowColor],[UIColor orangeColor],[UIColor redColor], nil];
for (int i = 0; i < 7; i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20 + i * 20 , 120 + i * 20, 280 - i * 20 * 2, 280 - i * 20 * 2)];
view.backgroundColor = arr[i];
view.tag = 100 + i;
[_containerView addSubview:view];
[view release];
}
//创建按钮控制霓虹灯停止
UIButton *bun = [UIButton buttonWithType:UIButtonTypeSystem];
bun.frame = CGRectMake(50, 420, 220, 40);
bun.layer.cornerRadius = 5;
[bun setTitle:@"停止" forState:UIControlStateNormal];
[bun addTarget:self action:@selector(bun:) forControlEvents:UIControlEventTouchUpInside];
[_containerView addSubview:bun];//创建让霓虹灯反向运行的按钮
UIButton *stop = [UIButton buttonWithType:UIButtonTypeSystem];
stop.frame = CGRectMake(50, 480, 220, 40);
stop.layer.cornerRadius = 5;
[stop setTitle:@"反向" forState:UIControlStateNormal];
[stop addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[_containerView addSubview:stop];
//设置时间间隔
_time = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(test) userInfo:nil repeats:YES];//停止事件
- (void)bun:(UIButton *)btn
{
[_time invalidate];
}
//正向运行点击事件
- (void)test
{
UIView *view = [[UIView alloc] init];
view.backgroundColor = [_containerView viewWithTag:100].backgroundColor;
for (int i = 100; i < 107; i++) {
[_containerView viewWithTag:i].backgroundColor = [_containerView viewWithTag:i + 1].backgroundColor;
}
[_containerView viewWithTag:106].backgroundColor = view.backgroundColor;
[view release];
}
//反向运行点击事件
- (void)click
{
_time = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(clicks) userInfo:nil repeats:YES];
}
- (void)clicks
{
UIView *view = [[UIView alloc] init];
view.backgroundColor = [_containerView viewWithTag:106].backgroundColor;
for (int i = 106; i > 99; i--) {
[_containerView viewWithTag:i].backgroundColor = [_containerView viewWithTag:i - 1].backgroundColor;
}
[_containerView viewWithTag:100].backgroundColor = view.backgroundColor;
[view release];
}
通过以上步骤,就基本完成了一个可以正向,反向,以及停止的霓虹灯制作.
原文地址:http://blog.csdn.net/w_sx_/article/details/38776293