我的第一个UI程序

来源:互联网 发布:曹变蛟 知乎 编辑:程序博客网 时间:2024/04/29 11:48

最近开始学习UI了,下面是我做的第一个小程序,感觉还不错,就是代码太冗余了,希望有专业人士给予指点

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   

    

    //创建window窗体

    //拿到设备的屏幕大小

    

    CGRect rect = [UIScreenmainScreen].bounds;

    

    //创建一个window窗体,使其大小等于屏幕大小

    

   self.window = [[UIWindowalloc]initWithFrame:rect];

    //设置窗体背景颜色

    self.window.backgroundColor = [UIColorredColor];

    

    //把当前设置好的window窗体当做主窗体显示出来

    [self.windowmakeKeyAndVisible];

    

    

    

    //创建七个视图做成嵌套的方形

    

    

    //第一层视图---->红色

   UIView *view1 = [[UIViewalloc]initWithFrame:CGRectMake(80,200, 260, 260)];

    //设置tag

    view1.tag =1;

    view1.backgroundColor = [UIColorredColor];

    view1.backgroundColor = [UIColorredColor];

    [self.windowaddSubview:view1];

    


    

    //第二层视图:橙色

   UIView *view2 = [[UIViewalloc]initWithFrame:CGRectMake(20,20, 220, 220)];

    view2.tag =2;

    view2.backgroundColor = [UIColororangeColor];

    [view1addSubview:view2];

    

    //第三层视图:黄色

   UIView *view3 = [[UIViewalloc]initWithFrame:CGRectMake(20,20, 180, 180)];

    view3.backgroundColor = [UIColoryellowColor];

    view3.tag =3;

    [view2addSubview:view3];

    

    //第四层视图:绿色

   UIView *view4 = [[UIViewalloc]initWithFrame:CGRectMake(20,20, 140, 140)];

    view4.backgroundColor = [UIColorgreenColor];

    view4.tag =4;

    [view3addSubview:view4];

    

    //第五层视图:蓝色

   UIView *view5 = [[UIViewalloc]initWithFrame:CGRectMake(20,20, 100, 100)];

    view5.backgroundColor = [UIColorblueColor];

    view5.tag =5;

    [view4addSubview:view5];

    

    

    //第六层视图:青色

   UIView *view6 = [[UIViewalloc]initWithFrame:CGRectMake(20,20, 60, 60)];

    view6.backgroundColor = [UIColorcyanColor];

    view6.tag =6;

    [view5addSubview:view6];

    

    //第七层视图:紫色

   UIView *view7 = [[UIViewalloc]initWithFrame:CGRectMake(20,20, 20, 20)];

    view7.backgroundColor = [UIColorpurpleColor];

    view7.tag =7;

    [view6addSubview:view7];

    

    

    

    

    [NSTimerscheduledTimerWithTimeInterval:1target:self

     selector:@selector(Turn:)

                                  userInfo:nilrepeats:YES];

    

    

    

    //初始化三个按钮

   //旋转

    UIButton *btn1 = [UIButtonbuttonWithType:UIButtonTypeSystem];

    [btn1 setTitle:@"旋转"forState:UIControlStateNormal];

    btn1.backgroundColor = [UIColorblackColor];

    btn1.frame =CGRectMake(80,600, 50, 40);

    [btn1 addTarget:selfaction:@selector(btnClick1:)forControlEvents:UIControlEventTouchUpInside];

    [self.windowaddSubview:btn1];

    

   //放大

    UIButton *btn2 = [UIButtonbuttonWithType:UIButtonTypeSystem];

    [btn2 setTitle:@"放大"forState:UIControlStateNormal];

    btn2.backgroundColor = [UIColorcyanColor];

    btn2.frame =CGRectMake(200,600, 50, 40);

    [btn2 addTarget:selfaction:@selector(btnClick2:)forControlEvents:UIControlEventTouchUpInside];

    [self.windowaddSubview:btn2];

    

   //缩小

    UIButton *btn3 = [UIButtonbuttonWithType:UIButtonTypeSystem];

    [btn3 setTitle:@"缩小"forState:UIControlStateNormal];

    btn3.backgroundColor = [UIColorblueColor];

    btn3.frame =CGRectMake(300,600, 50, 40);

    [btn3 addTarget:selfaction:@selector(btnClick3:)forControlEvents:UIControlEventTouchUpInside];

    [self.windowaddSubview:btn3];

    

    

    return YES;

}


- (void)btnClick1:(UIButton *)btn

{

    

   UIView *view = [self.windowviewWithTag:1];

    CGAffineTransform t = view.transform;

    

   //旋转

    view.transform =CGAffineTransformRotate(t, M_1_PI/4);

    

    

}



- (void)btnClick2:(UIButton *)btn

{

    

   UIView *view = [self.windowviewWithTag:1];

    CGAffineTransform t = view.transform;

    

   //放大

    //在原来的基础上进行放放-->每一次都会缩放1.5

    view.transform =CGAffineTransformScale(t, 1.5,1.5);

    

    

}



- (void)btnClick3:(UIButton *)btn

{

    

   UIView *view = [self.windowviewWithTag: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.windowviewWithTag:1];

   UIView *view2 = [self.windowviewWithTag:2];

   UIView *view3 = [self.windowviewWithTag:3];

   UIView *view4 = [self.windowviewWithTag:4];

   UIView *view5 = [self.windowviewWithTag:5];

   UIView *view6 = [self.windowviewWithTag:6];

   UIView *view7 = [self.windowviewWithTag:7];

    view1.backgroundColor = [UIColorcolorWithRed:(arc4random()%255/255.0)green:(arc4random()%255/255.0)blue:(arc4random()%255/255.0)alpha:1];

    view2.backgroundColor = [UIColorcolorWithRed:(arc4random()%255/255.0)green:(arc4random()%255/255.0)blue:(arc4random()%255/255.0)alpha:1];

    view3.backgroundColor = [UIColorcolorWithRed:(arc4random()%255/255.0)green:(arc4random()%255/255.0)blue:(arc4random()%255/255.0)alpha:1];

    view4.backgroundColor = [UIColorcolorWithRed:(arc4random()%255/255.0)green:(arc4random()%255/255.0)blue:(arc4random()%255/255.0)alpha:1];

    view5.backgroundColor = [UIColorcolorWithRed:(arc4random()%255/255.0)green:(arc4random()%255/255.0)blue:(arc4random()%255/255.0)alpha:1];

    view6.backgroundColor = [UIColorcolorWithRed:(arc4random()%255/255.0)green:(arc4random()%255/255.0)blue:(arc4random()%255/255.0)alpha:1];

    view7.backgroundColor = [UIColorcolorWithRed:(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];

    }

    }

运行程序的效果图






0 0
原创粉丝点击