iOS之UI编程--霓虹灯的实现

本文介绍了一种实现交互式霓虹灯效果的方法,包括由内而外、由外而内、暂停等功能。通过使用Objective-C编程语言,创建了一个包含多个颜色视图的界面,并实现了简单的定时器来控制颜色变化。同时提供了关键代码片段和实现细节。
霓虹灯。实现的功能可以由里而外,也可以由外到里,还可以暂停。由于水平有限,再加上还没有学习NSTimer,只是从网上了解到了简单地应用,所以代码实现上可能会有些问题,算法不是最优的,还望大神们批评指点。贴上一张截图:


下面附上代码实现:

  1. #import "ZZAppDelegate.h"  
  2.   
  3. @interface ZZAppDelegate ()  
  4. {  
  5.     UIView *_containerView;  
  6.     NSTimer *_timer;  
  7. }  
  8. @end  
  9. @implementation ZZAppDelegate  
  10.   
  11. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  12. {  
  13.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  14.     // Override point for customization after application launch.  
  15.     self.window.backgroundColor = [UIColor grayColor];  
  16.     _containerView = [[UIView alloc] initWithFrame:CGRectMake(00320568)];  
  17.     _containerView.backgroundColor = [UIColor clearColor];  
  18.     [self.window addSubview:_containerView];  
  19.     [_containerView release];  
  20.     // 视图布局  
  21.     NSArray *array = [NSArray arrayWithObjects:[UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor blueColor], [UIColor cyanColor], [UIColor purpleColor], nil nil];  
  22.   
  23.         for (int i = 0; i < 7; i++) {  
  24.             UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20 + i * 20120 + i * 20280 - i * 40280 - i * 40)];  
  25.             view.backgroundColor = array[i];  
  26.             view.tag = i + 200;  
  27.             NSLog(@"%ld", (long)view.tag);  
  28.             [_containerView addSubview:view];  
  29.             [view release];  
  30.         }  
  31.     // 创建由内而外按钮  
  32.     UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem  
  33.                          ];  
  34.     button1.frame = CGRectMake(404507040);  
  35.     button1.backgroundColor = [UIColor cyanColor];  
  36.     [button1 setTitle:@"由内而外" forState:UIControlStateNormal];  
  37.     [button1 addTarget:self action:@selector(changeFromInToOut) forControlEvents:UIControlEventTouchUpInside];  
  38.     button1.layer.cornerRadius = 7;  
  39.     [_containerView addSubview:button1];  
  40.     // 创建停止按钮  
  41.     UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem  
  42.                         ];  
  43.     button2.frame = CGRectMake(1254507040);  
  44.     button2.backgroundColor = [UIColor cyanColor];  
  45.     [button2 setTitle:@"停止" forState:UIControlStateNormal];  
  46.     [button2 addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];  
  47.     button2.layer.cornerRadius = 7;  
  48.     [_containerView addSubview:button2];  
  49.     // 创建由外而内按钮  
  50.     UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem  
  51.                          ];  
  52.     button3.frame = CGRectMake(2104507040);  
  53.     button3.backgroundColor = [UIColor cyanColor];  
  54.     [button3 setTitle:@"由外而内" forState:UIControlStateNormal];  
  55.     [button3 addTarget:self action:@selector(changeFromOutToIn) forControlEvents:UIControlEventTouchUpInside];  
  56.     button3.layer.cornerRadius = 7;  
  57.     [_containerView addSubview:button3];  
  58.     [self.window makeKeyAndVisible];  
  59.     return YES;  
  60. }  
  61. // 由内而外按钮触发事件  
  62. - (void)changeFromInToOut  
  63. {  
  64.     _timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(changeInToOut) userInfo:nil repeats:YES];  
  65. }  
  66. // 由外而内按钮出触发事件  
  67. - (void)changeFromOutToIn  
  68. {  
  69.     _timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(changeOutToIn) userInfo:nil repeats:YES];  
  70. }  
  71. // 暂停计时器  
  72. - (void)stop  
  73. {  
  74.     [_timer invalidate];  
  75. }  
  76. // 由内而外循环交换颜色  
  77. - (void)changeInToOut  
  78. {  
  79.     UIView *temp = [[UIView alloc] init];  
  80.     int i = 200;  
  81.     temp.backgroundColor = [_containerView viewWithTag:200].backgroundColor;  
  82.     for (i = 200; i < 207; i++) {  
  83.         [_containerView viewWithTag:i].backgroundColor = [_containerView viewWithTag: i + 1].backgroundColor;  
  84.     }  
  85.     [_containerView viewWithTag:206].backgroundColor =temp.backgroundColor;  
  86.     [temp release];  
  87. }  
  88. // 由外而内循环交换颜色  
  89. - (void)changeOutToIn  
  90. {  
  91.     UIView *temp = [[UIView alloc] init];  
  92.     int i = 206;  
  93.     temp.backgroundColor = [_containerView viewWithTag:206].backgroundColor;  
  94.     for (i = 206; i >= 0; i--) {  
  95.         [_containerView viewWithTag:i].backgroundColor = [_containerView viewWithTag: i - 1].backgroundColor;  
  96.     }  
  97.     [_containerView viewWithTag:200].backgroundColor =temp.backgroundColor;  
  98.     [temp release];  
  99. }  
  100.   
  101. - (void)dealloc  
  102. {  
  103.     [_window release];  
  104.     [super dealloc];  
  105. }  
  106. @end 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值