霓虹灯截图:
我的思路是:首先新建一个视图控制器,在这个控制器里定义一个成员变量类型是可变数组,数组中存放8种颜色;在视图已加载ViewDidLoad中使用定时器NSTimer调用两个方法,一个是在视图控制器上铺7个小View,然后分别给每个View添加背景颜色;另一个方法是让数组元素调换顺序,实现霓虹灯效果。
代码如下:
给成员变量初始化
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.arrayColor = [[NSMutableArray alloc] initWithObjects:[UIColor redColor],[UIColor yellowColor],[UIColor greenColor], [UIColor orangeColor], [UIColor blueColor], [UIColor brownColor], [UIColor purpleColor], [UIColor cyanColor], nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(tinyView) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(cartoon) userInfo:nil repeats:YES];
}
铺7个小试图
- (void)tinyView
{
NSUInteger j = 0;
for (int i = 0; i < [self.arrayColor count]; i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0 + (j / 2), 0 + (j / 2), 320 - j, 480 - j)];
view.backgroundColor = self.arrayColor[i];
j += 40;
[self.view addSubview:view];
[view release];
}
}
交换数组颜色
- (void)cartoon
{
NSString *temp = self.arrayColor[[self.arrayColor count] - 1];
for (NSUInteger i = [self.arrayColor count] - 1; i > 0; i--) {
self.arrayColor[i] = self.arrayColor[i - 1];
}
self.arrayColor[0] = temp;
}