我的第一个UI程序

本文分享了一位初学者在学习UI编程过程中制作的第一个小程序,通过逐步搭建视图层级和实现基本交互功能,展示了如何利用Objective-C语言创建动态、美观的用户界面。文章特别强调了代码冗余问题,并提供了优化建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近开始学习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];

    }

    }

运行程序的效果图






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值