AppDelegate.m文件
//
// AppDelegate.m
// UI_2_HomeWork
//
// Created by 0101 on 13-7-28.
// Copyright (c) 2013年 PH. All rights reserved.
//
#import "AppDelegate.h"
#import "Neon.h"
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
Neon *h = [[Neon alloc]init];
self.window.rootViewController = h;
[h release];
[self.window makeKeyAndVisible];
return YES;
}
Neon.m文件
//
// Neon.m
// UI_2_HomeWork
//
// Created by 0101 on 13-7-28.
// Copyright (c) 2013年 PH. All rights reserved.
//
//霓虹灯
#import "Neon.h"
#define KFrame(x,y,w,h) CGRectMake(x,y,w,h)
@implementation Neon
//加载视图
-(void)loadView
{
//初始化根视图 方式一,这种方式效率更高
self.view = [[[UIView alloc]initWithFrame:KFrame(0, 0, 320, 480)]autorelease];
//将颜色存储在数组中
arrColor = [[NSMutableArray alloc]initWithObjects:
[UIColor redColor],
[UIColor blueColor],
[UIColor yellowColor],
[UIColor purpleColor],
nil];
//使用for循环连续创建四个view
for (int i = 0 ; i < 4 ; i++ )
{
UIView *aView = [[UIView alloc]initWithFrame:KFrame(i*50+10, 50, 50, 50)];
aView.tag = i + 1000;
aView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:aView];
[aView release];
}
//创建一个定时器用于连续的改变颜色
NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(changeColor:)
userInfo:nil
repeats:YES];
}
//改变颜色消息
-(void)changeColor:(NSTimer *)aTimer
{
//将aView依据Tag赋给bView
for (int i = 0 ; i < 4 ; i++)
{
UIView *bView = [self.view viewWithTag:1000 + i];
//将arrColor中的颜色对象赋给bView.backgroundColor
bView.backgroundColor = [arrColor objectAtIndex:i];
}
//创建一个新的颜色对象,并将arrColor中最后一个颜色赋给newColor
UIColor *newColor = [arrColor lastObject];
//为防止arrColor数组不断的增大应删除最后一个对象,使数组总保持四个对象的容量
[arrColor removeLastObject];
//同时将最后一个对象插入到最前面
[arrColor insertObject:newColor atIndex:0];
}
-(void)viewDidLoad
{
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end