标签:
总体思路如下:
利用for 循环创建N个view,并将他们添加到可变数组中,利用NSTimer,实现循环变色的效果(changeColor:),变色的思路:将第i个view 的背景色 和第i + 1 个view 的背景色交换,换完一轮即可,(NSTimer 会自动实现循环)
这里是在试图控制器中写的,所以 把view 添加到self.view里, self.view.subviews(是一个数组(里边是各个VIEW))
- (void)viewDidLoad {
[super viewDidLoad];
for (int i = 0; i < 6; i++) {
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(i * 20, i * 40, self.view.frame.size.width - i * 40, self.view.frame.size.height - i * 80)];//创建VIEW
view.backgroundColor = [UIColor colorWithRed: arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];//背景颜色随机赋值
[self.view addSubview:view];//将其添加到(数组)
[view release];
}
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:.8 target:self selector:@selector(changeColor:) userInfo:self.view.subviews repeats:YES];//一直循环(把self.view.subviews 传到(changeColor:)里)
[timer fire];
- (void)changeColor:(NSTimer *)sender
{
NSMutableArray * ary = [NSMutableArray arrayWithArray:sender.userInfo];
UIView * v = [[UIView alloc] init];
for (int i = 0; i < ary.count - 1; i++) {//将第i个view 的背景色 和第i + 1 个view 的背景色交换
UIView * v1 = [ary objectAtIndex:i];
UIView * v2 = [ary objectAtIndex:i + 1];
v.backgroundColor = v1.backgroundColor;
v1.backgroundColor = v2.backgroundColor;
v2.backgroundColor = v.backgroundColor;
}
}
标签:
原文地址:http://www.cnblogs.com/lzcdbk/p/4734795.html