最近开始学习UI了,下面是我做的第一个小程序,感觉还不错,就是代码太冗余了,希望有专业人士给予指点
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建window窗体
//拿到设备的屏幕大小
CGRect rect = [UIScreen mainScreen].bounds;
//创建一个window窗体,使其大小等于屏幕大小
self.window = [[UIWindow alloc]initWithFrame:rect];
//设置窗体背景颜色
self.window.backgroundColor = [UIColor redColor];
//把当前设置好的window窗体当做主窗体显示出来
[self.window makeKeyAndVisible];
//创建七个视图做成嵌套的方形
//第一层视图---->红色
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(80, 200, 260, 260)];
//设置tag值
view1.tag = 1;
view1.backgroundColor = [UIColor redColor];
view1.backgroundColor = [UIColor redColor];
[self.window addSubview:view1];
//第二层视图:橙色
UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 220, 220)];
view2.tag = 2;
view2.backgroundColor = [UIColor orangeColor];
[view1 addSubview:view2];
//第三层视图:黄色
UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 180, 180)];
view3.backgroundColor = [UIColor yellowColor];
view3.tag = 3;
[view2 addSubview:view3];
//第四层视图:绿色
UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 140, 140)];
view4.backgroundColor = [UIColor greenColor];
view4.tag = 4;
[view3 addSubview:view4];
//第五层视图:蓝色
UIView *view5 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 100, 100)];
view5.backgroundColor = [UIColor blueColor];
view5.tag = 5;
[view4 addSubview:view5];
//第六层视图:青色
UIView *view6 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 60, 60)];
view6.backgroundColor = [UIColor cyanColor];
view6.tag = 6;
[view5 addSubview:view6];
//第七层视图:紫色
UIView *view7 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 20, 20)];
view7.backgroundColor = [UIColor purpleColor];
view7.tag = 7;
[view6 addSubview:view7];
[NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:@selector(Turn:)
userInfo:nil repeats:YES];
//初始化三个按钮
//旋转
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
[btn1 setTitle:@"旋转" forState:UIControlStateNormal];
btn1.backgroundColor = [UIColor blackColor];
btn1.frame = CGRectMake(80, 600, 50, 40);
[btn1 addTarget:self action:@selector(btnClick1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btn1];
//放大
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
[btn2 setTitle:@"放大" forState:UIControlStateNormal];
btn2.backgroundColor = [UIColor cyanColor];
btn2.frame = CGRectMake(200, 600, 50, 40);
[btn2 addTarget:self action:@selector(btnClick2:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btn2];
//缩小
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeSystem];
[btn3 setTitle:@"缩小" forState:UIControlStateNormal];
btn3.backgroundColor = [UIColor blueColor];
btn3.frame = CGRectMake(300, 600, 50, 40);
[btn3 addTarget:self action:@selector(btnClick3:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btn3];
return YES;
}
- (void)btnClick1:(UIButton *)btn
{
UIView *view = [self.window viewWithTag:1];
CGAffineTransform t = view.transform;
//旋转
view.transform = CGAffineTransformRotate(t, M_1_PI/4);
}
- (void)btnClick2:(UIButton *)btn
{
UIView *view = [self.window viewWithTag:1];
CGAffineTransform t = view.transform;
//放大
//在原来的基础上进行放放-->每一次都会缩放1.5倍
view.transform = CGAffineTransformScale(t, 1.5, 1.5);
}
- (void)btnClick3:(UIButton *)btn
{
UIView *view = [self.window viewWithTag:1];
CGAffineTransform t = view.transform;
//缩放
//在原来的基础上进行缩放-->每一次都会缩放0.5倍
view.transform = CGAffineTransformScale(t, 0.5, 0.5);
}
int t;
//让图形旋转的方法
- (void)Turn:(NSTimer *)timer
{
t ++;
//可以随机变换颜色
UIView *view1 = [self.window viewWithTag:1];
UIView *view2 = [self.window viewWithTag:2];
UIView *view3 = [self.window viewWithTag:3];
UIView *view4 = [self.window viewWithTag:4];
UIView *view5 = [self.window viewWithTag:5];
UIView *view6 = [self.window viewWithTag:6];
UIView *view7 = [self.window viewWithTag:7];
view1.backgroundColor = [UIColor colorWithRed:(arc4random()%255/255.0) green:(arc4random()%255/255.0) blue:(arc4random()%255/255.0) alpha:1];
view2.backgroundColor = [UIColor colorWithRed:(arc4random()%255/255.0) green:(arc4random()%255/255.0) blue:(arc4random()%255/255.0) alpha:1];
view3.backgroundColor = [UIColor colorWithRed:(arc4random()%255/255.0) green:(arc4random()%255/255.0) blue:(arc4random()%255/255.0) alpha:1];
view4.backgroundColor = [UIColor colorWithRed:(arc4random()%255/255.0) green:(arc4random()%255/255.0) blue:(arc4random()%255/255.0) alpha:1];
view5.backgroundColor = [UIColor colorWithRed:(arc4random()%255/255.0) green:(arc4random()%255/255.0) blue:(arc4random()%255/255.0) alpha:1];
view6.backgroundColor = [UIColor colorWithRed:(arc4random()%255/255.0) green:(arc4random()%255/255.0) blue:(arc4random()%255/255.0) alpha:1];
view7.backgroundColor = [UIColor colorWithRed:(arc4random()%255/255.0) green:(arc4random()%255/255.0) blue:(arc4random()%255/255.0) alpha:1];
// view1.transform = CGAffineTransformRotate(view1.transform, M_PI / 8);
if (t == 10) {
[view1 removeFromSuperview];
}
}
运行程序的效果图