首页 > 代码库 > 霓虹灯的制作(正向,反向,以及停止)
霓虹灯的制作(正向,反向,以及停止)
//先创建一个霓虹灯视图 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]; }
通过以上步骤,就基本完成了一个可以正向,反向,以及停止的霓虹灯制作.
file:///Users/lanouhn/Desktop/1.gif
霓虹灯的制作(正向,反向,以及停止)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。