训练营 - 使用UIView模拟霓虹灯效果

霓虹灯

1 -代码示例:可开启、终止霓虹灯闪动;可控制霓虹灯闪动方向

① 方式一:循环遍历视图

复制代码
  1 #import "ViewController.h"
  2 #define W_Main  [UIScreen mainScreen].bounds.size.width
  3 @interface ViewController (){
  4     NSTimer *_testTimer;
  5 }
  6 
  7 @end
  8 
  9 @implementation ViewController
 10 
 11 - (void)viewDidLoad {
 12     [super viewDidLoad];
 13     self.view.backgroundColor = [UIColor whiteColor];
 14 
 15     // 开启
 16     UIButton *swBT = [UIButton buttonWithType:UIButtonTypeCustom];
 17     swBT.backgroundColor = [UIColor blackColor];
 18     swBT.frame = CGRectMake(100, 480, W_Main-100*2, 50);
 19     [swBT setTitle:@"Start" forState:UIControlStateNormal];
 20     [swBT addTarget:self action:@selector(startRuning:) forControlEvents:UIControlEventTouchUpInside];
 21     swBT.layer.cornerRadius = 25;
 22     swBT.tag = 199;
 23     [self.view addSubview:swBT];
 24 
 25     // 停止
 26     UIButton *stBT = [UIButton buttonWithType:UIButtonTypeCustom];
 27     stBT.backgroundColor = [UIColor redColor];
 28     stBT.frame = CGRectMake(135, 580, W_Main - 270, 50);
 29     [stBT setTitle:@"Stop" forState:UIControlStateNormal];
 30     [stBT addTarget:self action:@selector(stopRuning:) forControlEvents:UIControlEventTouchUpInside];
 31     stBT.layer.cornerRadius = 25;
 32     [self.view addSubview:stBT];
 33 
 34     // 颜色
 35     NSArray *array = [NSArray arrayWithObjects:
 36                       [UIColor darkGrayColor],
 37                       [UIColor yellowColor],
 38                       [UIColor redColor],
 39                       [UIColor greenColor],
 40                       [UIColor purpleColor],
 41                       [UIColor cyanColor],
 42                       [UIColor blueColor], nil];
 43 
 44     // 子视图
 45     for (int i = 0; i < array.count; i++) {
 46         UIView *view_i = [[UIView alloc] initWithFrame:CGRectMake(20 + 20*i, 140 + 20*i, W_Main-20*2 - 40*i, 280 - 40*i)];
 47         view_i.layer.cornerRadius = 10;
 48         view_i.backgroundColor = array[i];// 颜色
 49         view_i.tag = 101 + i; // tag 值
 50         [self.view addSubview:view_i];
 51     }
 52 }
 53 
 54 // 停止
 55 -(void)stopRuning:(UIButton*)bt{
 56 
 57     if (_testTimer) {
 58         [_testTimer invalidate];
 59         _testTimer = nil;
 60 
 61         UIButton *statusBT = (UIButton*)[self.view viewWithTag:199];
 62         [statusBT setTitle:@"Start" forState:UIControlStateNormal];
 63     }
 64 
 65 }
 66 
 67 // 开启
 68 - (void)startRuning:(UIButton*)bt{
 69 
 70     if ([bt.titleLabel.text isEqualToString:@"Start"]) {
 71 
 72         if (!_testTimer) {
 73             _testTimer =  [NSTimer scheduledTimerWithTimeInterval:.8 target:self selector:@selector(runing01) userInfo:self.view repeats:YES];
 74             [bt setTitle:@"由内向外" forState:UIControlStateNormal];
 75 
 76         }
 77 
 78     }else if ([bt.titleLabel.text isEqualToString:@"由内向外"]){
 79 
 80         if (_testTimer) {
 81             [_testTimer invalidate];
 82             _testTimer = nil;
 83             _testTimer =  [NSTimer scheduledTimerWithTimeInterval:.8 target:self selector:@selector(runing02) userInfo:self.view repeats:YES];
 84             [bt setTitle:@"由外向内" forState:UIControlStateNormal];
 85         }
 86 
 87     }else if([bt.titleLabel.text isEqualToString:@"由外向内"]){
 88 
 89         if (_testTimer) {
 90             [_testTimer invalidate];
 91             _testTimer = nil;
 92             _testTimer =  [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(runing01) userInfo:self.view repeats:YES];
 93             [bt setTitle:@"由内向外" forState:UIControlStateNormal];
 94         }
 95     }else{
 96         // nothing todo.........
 97     }
 98 
 99 }
100 
101 // 由内向外
102 - (void)runing01{
103 
104     // 搞一个中间变量
105     UIColor *changeColor = [self.view viewWithTag:101].backgroundColor;
106     for (int i = 0; i < 6; i ++) {
107         // 颜色替换
108         [self.view viewWithTag:101 + i].backgroundColor = [self.view viewWithTag:101 + i +1].backgroundColor;
109         if (i == 5) {
110             [self.view viewWithTag:107].backgroundColor = changeColor;
111         }
112     }
113 }
114 
115 // 由外向内
116 - (void)runing02{
117 
118     // 同样搞一个中间变量
119     UIColor *changeColor = [self.view viewWithTag:107].backgroundColor;
120     for (int i = 6;i > 0; i --) {
121         // 颜色的替换
122         [self.view viewWithTag:i+101].backgroundColor = [self.view viewWithTag: i+100].backgroundColor;
123         if (i ==1 ) {
124             [self.view viewWithTag:101].backgroundColor = changeColor;
125         }
126     }
127 
128 }
129 
130 @end
复制代码

运行效果

② 方式二:手动布局视图(加深理解其实现原理)

复制代码
 1 #import "ViewController.h"
 2 #define W_Main  [UIScreen mainScreen].bounds.size.width
 3 @interface ViewController (){
 4     UIView *aView;
 5     UIView *bView;
 6     UIView *cView;
 7     UIView *dView;
 8     UIView *eView;
 9 }
10 @end
11 
12 @implementation ViewController
13 
14 - (void)viewDidLoad {
15     [super viewDidLoad];
16     self.view.backgroundColor = [UIColor whiteColor];
17 
18     // 间隔 20
19     int space = 20;
20 
21     aView = [[UIView alloc] initWithFrame:CGRectMake(70, 60,W_Main-140, 280)];
22     aView.backgroundColor = [UIColor redColor];
23     [self.view addSubview:aView];
24     aView.tag = 101;
25 
26     bView = [[UIView alloc] initWithFrame:CGRectMake(70+space, 80, W_Main-180,240)];
27     bView.backgroundColor = [UIColor orangeColor];
28     [self.view addSubview:bView];
29     bView.tag = 102;
30 
31     cView = [[UIView alloc] initWithFrame:CGRectMake(70+space*2, 100, W_Main-220, 200)];
32     cView.backgroundColor = [UIColor yellowColor];
33     [self.view addSubview:cView];
34     cView.tag = 103;
35 
36     dView = [[UIView alloc] initWithFrame:CGRectMake(70+space*3, 120, W_Main-260, 160)];
37     dView.backgroundColor = [UIColor greenColor];
38     [self.view addSubview:dView];
39     dView.tag = 104;
40 
41     eView = [[UIView alloc] initWithFrame:CGRectMake(70+space*4, 140, W_Main-300, 120)];
42     eView.backgroundColor = [UIColor purpleColor];
43     [self.view addSubview:eView];
44     eView.tag =105;
45 
46     // NSTimer
47     [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(exchangeViewColor) userInfo:nil repeats:YES];
48 }
49 
50 - (void)exchangeViewColor{
51 
52 //    // 由内向外
53 //    UIColor *changeColor11 = aView.backgroundColor;
54 //    aView.backgroundColor = bView.backgroundColor;
55 //    bView.backgroundColor = cView.backgroundColor;
56 //    cView.backgroundColor = dView.backgroundColor;
57 //    dView.backgroundColor = eView.backgroundColor;
58 //    eView.backgroundColor = changeColor11;
59 
60     // 由外向内
61     UIColor *changeColor22 = eView.backgroundColor;
62     eView.backgroundColor = dView.backgroundColor;
63     dView.backgroundColor = cView.backgroundColor;
64     cView.backgroundColor = bView.backgroundColor;
65     bView.backgroundColor = aView.backgroundColor;
66     aView.backgroundColor = changeColor22;
67 
68 }
69 
70 @end
复制代码

运行效果

 

 

posted on   低头捡石頭  阅读(22)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示