學習 UI 編程已經三四天了,這段時間接觸了一些基礎的視圖與控制項,例如 UIView、UILabel 以及 UIButton。UI 編程最有趣的地方在於——自己寫下的程式碼能立刻看到畫面上的成果,這一點和 C 語言或純 Objective-C 的學習體驗很不一樣。
於是我就動手做了一個視圖顏色變換的小應用,也就是俗稱的「霓虹燈」。它能做到由內而外、由外而內的循環變換,還可以隨時暫停。由於當時還沒正式學過 NSTimer,只是上網了解了一些簡單用法,因此程式碼在演算法上未必是最優解,也歡迎各路高手批評指正。先貼一張成品截圖:

整體做法很直觀:先建立七層錯開排列、顏色各異的方形視圖,並為每一層設定不同的 tag;接著透過 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]];
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;
[_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];
temp.backgroundColor = [_containerView viewWithTag:200].backgroundColor;
for (int i = 200; i < 206; 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];
temp.backgroundColor = [_containerView viewWithTag:206].backgroundColor;
for (int i = 206; i > 200; i--) {
[_containerView viewWithTag:i].backgroundColor = [_containerView viewWithTag:i - 1].backgroundColor;
}
[_containerView viewWithTag:200].backgroundColor = temp.backgroundColor;
[temp release];
}
@end
整個效果關鍵在於兩點:一是利用 tag 標記每一層視圖,方便用 viewWithTag: 快速取得對應視圖;二是透過 NSTimer 定時觸發顏色的「整體位移」,模擬出光點逐層流動的霓虹燈感覺。這個小練習很適合初學者熟悉視圖層級、tag 標記與計時器的基本用法。