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

来源:互联网 发布:北京租房软件 推荐 编辑:程序博客网 时间:2024/05/29 03:44
#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;}
0 0