首先创建一个视图NioLampViewController类
AppDelegate.h
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
#import "AppDelegate.h" #import "NioLampViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; //创建并且指定一个控制器 NioLampViewController *mainVC = [[NioLampViewController alloc] init]; self.window.rootViewController = mainVC; [_window release]; return YES; } - (void)dealloc { [_window release]; [super dealloc]; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
NioLampViewController.h
#import <UIKit/UIKit.h> @interface NioLampViewController : UIViewController @end
NioLampViewController.m
#import "NioLampViewController.h" @interface NioLampViewController () @end @implementation NioLampViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 50, 280, 280)]; view1.backgroundColor = [UIColor yellowColor]; view1.layer.cornerRadius = 140; view1.tag = 100; [self.view addSubview:view1]; [view1 release]; UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(40, 70, 240, 240)]; view2.backgroundColor = [UIColor magentaColor]; view2.layer.cornerRadius = 120; view2.tag = 250; [self.view addSubview:view2]; [view2 release]; UIView *view3 = [[UIView alloc] initWithFrame: CGRectMake(60, 90, 200, 200)]; view3.backgroundColor = [UIColor blueColor]; view3.layer.cornerRadius = 100; view3.tag = 300; [self.view addSubview:view3]; [view3 release]; UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(80, 110, 160, 160)]; view4.backgroundColor = [UIColor redColor]; view4.layer.cornerRadius = 80; view4.tag = 400; [self.view addSubview:view4]; [view4 release]; UIView *view5 = [[UIView alloc] initWithFrame:CGRectMake(100, 130, 120 , 120)]; view5.backgroundColor = [UIColor darkGrayColor]; view5.layer.cornerRadius = 60; view5.tag = 500; [self.view addSubview:view5]; [view5 release]; UIView *view6 = [[UIView alloc] initWithFrame:CGRectMake(120, 150, 80, 80)]; view6.backgroundColor = [UIColor orangeColor]; view6.layer.cornerRadius = 40; view6.tag = 600; [self.view addSubview:view6]; [view6 release]; UIView *view7 = [[UIView alloc] initWithFrame:CGRectMake(140, 170, 40, 40)]; view7.backgroundColor = [UIColor blackColor]; view7.layer.cornerRadius = 20; view7.tag = 700; [self.view addSubview:view7]; [view7 release]; [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerFired) userInfo:nil repeats:YES]; // NSTimer *myTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerFired) userInfo:nil repeats:YES]; // [myTimer fire]; // // NSTimer *timer = [NSTimer timerWithTimeInterval:0.3 target:self selector:@selector(timerFired) userInfo:nil repeats:YES]; // [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; // for (int i =0 ; i < 7; i++) { // // UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0 + i *20, 0 + i *20,320 - 40 * i ,568 - i * 40)]; // // CGFloat color = (arc4random() %256 /255.0 ); // // CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // // CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // CGFloat brightness1 = ( arc4random() % 56 / 256.0 ) + 0.5; // // view.backgroundColor = [UIColor colorWithHue:color saturation:saturation brightness:brightness alpha:brightness1]; // // [self.view addSubview:view]; // // [view release]; // // } // CGFloat color = (arc4random() %256 /255.0 ); // // CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // // CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // CGFloat brightness1 = ( arc4random() % 56 / 256.0 ) + 0.5; // view.backgroundColor = [UIColor colorWithHue:color saturation:saturation brightness:brightness alpha:brightness1] // // NSArray *array = [NSArray arrayWithObjects:[UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor blueColor], [UIColor cyanColor], [UIColor purpleColor], nil]; //方块 for (int i = 0; i < 11; i++) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100 + i * 5, 340 + i * 5, 100 - i * 10, 100 - i * 10)]; CGFloat color = (arc4random() %256 /255.0 ); CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; CGFloat brightness1 = ( arc4random() % 56 / 256.0 ) + 0.5; view.backgroundColor = [UIColor colorWithHue:color saturation:saturation brightness:brightness alpha:brightness1]; view.tag = i + 200; NSLog(@"%ld", (long)view.tag); [self.view addSubview:view]; [view release]; } [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(roll) userInfo:nil repeats:YES]; //竖条 for (int i = 0; i < 10; i++) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(300, 40 + i * 40, 20, 40)]; CGFloat color = (arc4random() %256 /255.0 ); CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; CGFloat brightness1 = ( arc4random() % 56 / 256.0 ) + 0.5; view.backgroundColor = [UIColor colorWithHue:color saturation:saturation brightness:brightness alpha:brightness1]; view.tag = i + 2000; NSLog(@"%ld", (long)view.tag); [self.view addSubview:view]; [view release]; } [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(shutiao) userInfo:nil repeats:YES]; } - (void)shutiao { UIColor *temp = [self.view viewWithTag:200].backgroundColor; for (int i = 2000; i < 2010; i++) { [self.view viewWithTag:i].backgroundColor = [self.view viewWithTag:i + 1].backgroundColor ; } [self.view viewWithTag:2009].backgroundColor = temp; } - (void)roll { UIColor *temp = [self.view viewWithTag:200].backgroundColor; for (int i = 200; i < 211; i++) { [self.view viewWithTag:i].backgroundColor = [self.view viewWithTag:i + 1].backgroundColor ; } [self.view viewWithTag:210].backgroundColor = temp; } -(void)timerFired { UIView *p1 = [self.view viewWithTag:100]; UIView *p2 = [self.view viewWithTag:250]; UIView *p3 = [self.view viewWithTag:300]; UIView *p4 = [self.view viewWithTag:400]; UIView *p5 = [self.view viewWithTag:500]; UIView *p6 = [self.view viewWithTag:600]; UIView *p7 = [self.view viewWithTag:700]; // [self.view viewWithTag:100].backgroundColor = [self.view viewWithTag:200].backgroundColor; UIColor *temp = p1.backgroundColor; p1.backgroundColor = p2.backgroundColor; p2.backgroundColor = p3.backgroundColor; p3.backgroundColor = p4.backgroundColor; p4.backgroundColor = p5.backgroundColor; p5.backgroundColor = p6.backgroundColor; p6.backgroundColor = p7.backgroundColor; p7.backgroundColor = temp; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end