新建一个工程,往window上添加赤橙黄绿青蓝紫七个视图 做成一个嵌套的方形,使用NSTimer每一秒钟换一下颜色,要求循环换颜色并发生旋转,待十秒后移除window上所有试图(刚学,只会用最简单的方法)
首先是创建视图,我是通过手写UIview来建立视图的
代码:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
view7=[[UIView alloc]initWithFrame:CGRectMake(120, 100, 80, 80)];
view7.backgroundColor=[UIColor purpleColor];
[self.window addSubview:view7];
view6=[[UIView alloc]initWithFrame:CGRectMake(120, 100, 80, 80)];
view6.backgroundColor=[UIColor cyanColor];
[self.window addSubview:view6];
view5=[[UIView alloc]initWithFrame:CGRectMake(120, 100, 80, 80)];
view5.backgroundColor=[UIColor blueColor];
[self.window addSubview:view5];
view4=[[UIView alloc]initWithFrame:CGRectMake(120, 100, 80, 80)];
view4.backgroundColor=[UIColor greenColor];
[self.window addSubview:view4];
view3=[[UIView alloc] initWithFrame:CGRectMake(120, 100, 80, 80)];
view3.backgroundColor=[UIColor yellowColor];
[self.window addSubview:view3];
view2=[[UIView alloc]initWithFrame:CGRectMake(120, 100, 80, 80)];
view2.backgroundColor=[UIColor orangeColor];
[self.window addSubview:view2];
view1=[[UIView alloc]initWithFrame:CGRectMake(120, 100, 80, 80)];
view1.backgroundColor=[UIColor redColor];
[self.window addSubview:view1];
if(1){
NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(changecolor:) userInfo:nil repeats:YES];
[timer fire];
}
return YES;
然后是判断什么时候改变视图颜色,即切换视图
代码:
-(void) changecolor:(NSTimer *)timer{
++i;
if (i==1||i==8) {
view1.transform=CGAffineTransformRotate(view1.transform, 0.33);
}
if(i==2||i==9) {
[self.window bringSubviewToFront:view2];
view2.transform=CGAffineTransformRotate(view1.transform, 0.33);
}
if (i==3||i==10) {
[self.window bringSubviewToFront:view3];
view3.transform=CGAffineTransformRotate(view3.transform, 0.33);
}
if (i==4) {
[self.window bringSubviewToFront:view4];
view4.transform=CGAffineTransformRotate(view4.transform, 0.33);
}
if (i==5) {
[self.window bringSubviewToFront:view5];
view5.transform=CGAffineTransformRotate(view5.transform, 0.33);
}
if (i==6) {
[self.window bringSubviewToFront:view6];
view6.transform=CGAffineTransformRotate(view6.transform, 0.33);
}
if (i==7) {
[self.window bringSubviewToFront:view7];
view7.transform=CGAffineTransformRotate(view7.transform, 0.33);
}
if(i==10){
for (UIView *view in self.window.subviews) {
[view removeFromSuperview];
}
}
所创建的view时全局变量,i是静态变量。
NSTimer是一个类,首先创建一个实例对象。
例如
NSTimer *timer1=[NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval) target:(id) selector:(SEL) userInfo:(id) repeats:(BOOL)];
//运行
<span style="font-family: Menlo;">[timer fire];</span>
其他很简单,看一下都懂了,NSTimer这个类说简单简单,说难又难,更加详细的可以上网查一下。
最后结尾,第一次写的东西虽然很简单,还是作为ios开发的第一次编程吧,人生总有第一次,不管怎样,先开个头吧