首页 > 代码库 > iOS之UI编程--霓虹灯的实现
iOS之UI编程--霓虹灯的实现
学习UI编程已经有三四天了,这几天学习了一些初级的视图和控件,比如UIView,UIlabel以及UIButton.,学习UI编程,有一点乐趣就是自己写的代码可以看到效果了。这一点和C语言以及OC区别很大。于是我就尝试做了一个简单视图颜色变换的应用,也就是霓虹灯。实现的功能可以由里而外,也可以由外到里,还可以暂停。由于水平有限,再加上还没有学习NSTimer,只是从网上了解到了简单地应用,所以代码实现上可能会有些问题,算法不是最优的,还望大神们批评指点。贴上一张截图:
下面附上代码实现:
#import "ZZAppDelegate.h" @interface ZZAppDelegate () { UIView *_containerView; NSTimer *_timer; } @end @implementation ZZAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor grayColor]; _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)]; _containerView.backgroundColor = [UIColor clearColor]; [self.window addSubview:_containerView]; [_containerView release]; // 视图布局 NSArray *array = [NSArray arrayWithObjects:[UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor blueColor], [UIColor cyanColor], [UIColor purpleColor], nil]; for (int i = 0; i < 7; i++) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20 + i * 20, 120 + i * 20, 280 - i * 40, 280 - i * 40)]; view.backgroundColor = array[i]; view.tag = i + 200; NSLog(@"%ld", (long)view.tag); [_containerView addSubview:view]; [view release]; } // 创建由内而外按钮 UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem ]; button1.frame = CGRectMake(40, 450, 70, 40); button1.backgroundColor = [UIColor cyanColor]; [button1 setTitle:@"由内而外" forState:UIControlStateNormal]; [button1 addTarget:self action:@selector(changeFromInToOut) forControlEvents:UIControlEventTouchUpInside]; button1.layer.cornerRadius = 7; [_containerView addSubview:button1]; // 创建停止按钮 UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem ]; button2.frame = CGRectMake(125, 450, 70, 40); button2.backgroundColor = [UIColor cyanColor]; [button2 setTitle:@"停止" forState:UIControlStateNormal]; [button2 addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside]; button2.layer.cornerRadius = 7; [_containerView addSubview:button2]; // 创建由外而内按钮 UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem ]; button3.frame = CGRectMake(210, 450, 70, 40); button3.backgroundColor = [UIColor cyanColor]; [button3 setTitle:@"由外而内" forState:UIControlStateNormal]; [button3 addTarget:self action:@selector(changeFromOutToIn) forControlEvents:UIControlEventTouchUpInside]; button3.layer.cornerRadius = 7; [_containerView addSubview:button3]; [self.window makeKeyAndVisible]; return YES; } // 由内而外按钮触发事件 - (void)changeFromInToOut { _timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(changeInToOut) userInfo:nil repeats:YES]; } // 由外而内按钮出触发事件 - (void)changeFromOutToIn { _timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(changeOutToIn) userInfo:nil repeats:YES]; } // 暂停计时器 - (void)stop { [_timer invalidate]; } // 由内而外循环交换颜色 - (void)changeInToOut { UIView *temp = [[UIView alloc] init]; int i = 200; temp.backgroundColor = [_containerView viewWithTag:200].backgroundColor; for (i = 200; i < 207; i++) { [_containerView viewWithTag:i].backgroundColor = [_containerView viewWithTag: i + 1].backgroundColor; } [_containerView viewWithTag:206].backgroundColor =temp.backgroundColor; [temp release]; } // 由外而内循环交换颜色 - (void)changeOutToIn { UIView *temp = [[UIView alloc] init]; int i = 206; temp.backgroundColor = [_containerView viewWithTag:206].backgroundColor; for (i = 206; i >= 0; i--) { [_containerView viewWithTag:i].backgroundColor = [_containerView viewWithTag: i - 1].backgroundColor; } [_containerView viewWithTag:200].backgroundColor =temp.backgroundColor; [temp release]; } - (void)dealloc { [_window release]; [super dealloc]; } @end
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。