UI之窗口与视图

2023-05-17,,

----------UI窗口于视图的创建示例----------

window上创建赤橙黄绿青蓝紫七个视图,互相嵌套,设置定时器,每秒每个视图随机变换颜色,并且旋转,十秒后停止,视图全部移除。

---AppDelegate.h中声明视图和一个计时的变量

@interface AppDelegate : UIResponder <UIApplicationDelegate>

{

    UIView *view1;

    UIView *view2;

    UIView *view3;

    UIView *view4;

    UIView *view5;

    UIView *view6;

    UIView *view7;

    int second;

}

---AppDelegate.m中实现题中要求

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

    CGRect rect = [UIScreen mainScreen].bounds;

    //创建主Window

    self.window = [[UIWindow alloc]initWithFrame:rect];

    self.window.backgroundColor = [UIColor blackColor];

    [self.window makeKeyAndVisible];

    

    //创建View

    view1 = [[UIView alloc]initWithFrame:CGRectMake(70, 70, 250, 250)];

    view1.backgroundColor= [UIColor redColor];

    view1.tag = 1;

    

    view2 = [[UIView alloc]initWithFrame:CGRectMake(15, 15, 220, 220)];

    view2.backgroundColor= [UIColor orangeColor];

    view3 = [[UIView alloc]initWithFrame:CGRectMake(15, 15, 190, 190)];

    view3.backgroundColor= [UIColor yellowColor];

    view4 = [[UIView alloc]initWithFrame:CGRectMake(15, 15, 160, 160)];

    view4.backgroundColor= [UIColor greenColor];

    view5 = [[UIView alloc]initWithFrame:CGRectMake(15, 15 , 130, 130)];

    view5.backgroundColor= [UIColor cyanColor];

    view6 = [[UIView alloc]initWithFrame:CGRectMake(15, 15, 100, 100)];

    view6.backgroundColor= [UIColor blueColor];

    view7 = [[UIView alloc]initWithFrame:CGRectMake(25, 25, 50, 50)];

    view7.backgroundColor= [UIColor purpleColor];

    [self.window addSubview:view1];

    [view1 addSubview:view2];

    [view2 addSubview:view3];

    [view3 addSubview:view4];

    [view4 addSubview:view5];

    [view5 addSubview:view6];

    [view6 addSubview:view7];

    

    second = 10;

    //定时器

    [NSTimer scheduledTimerWithTimeInterval:1

                                     target:self

                                   selector:@selector(timeAction:)

                                   userInfo:nil

                                    repeats:YES];

    

    

    

        return YES;

}

- (void)timeAction:(NSTimer *)timer{

    //七个视图颜色随机变

    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];

    

    //旋转

    UIView *view = [self.window viewWithTag:1];

    CGAffineTransform trans = view.transform;

    view.transform = CGAffineTransformRotate(trans, M_PI/10);

    

    

    //十秒后计时器停止,视图移除

    second--;

    if (second < 0) {

        [timer invalidate];

        [view1 removeFromSuperview];

        return;

    }

        

}

《UI之窗口与视图.doc》

下载本文的Word格式文档,以方便收藏与打印。