您的位置:首页 > 其它

颜色块由外到内的闪动NSTimer ...

2016-01-07 13:31 260 查看
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//生成几个视图并为几个视图添加颜色(从大到小)
for (NSInteger i = 7; i >= 1; i--) {
//只要不出界就行
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50 * i, 50 * i)];
//视图颜色随机
view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.0];
//视图中心点是屏幕的中心点
view.center = self.view.center;
//视图tag值
view.tag = 100 + i;
[self.view addSubview:view];
[view release];
}

//计时器, 调用改变颜色的函数
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(changeColor) userInfo:nil repeats:YES];
}

- (void)changeColor {
//交换视图6次
for (NSInteger i = 1; i <= 6; i++) {
UIView *view1 = (UIView *)[self.view viewWithTag:100 + i];
UIView *view2 = (UIView *)[self.view viewWithTag:100 + i + 1];
//大的赋给小的
view1.backgroundColor = view2.backgroundColor;
}

//大的再随机产生颜色view
UIColor *color = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.0];
UIView *view = (UIView *)[self.view viewWithTag:107];
view.backgroundColor = color;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: