温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

UI之窗口与视图

发布时间:2020-07-18 05:00:18 来源:网络 阅读:264 作者:hmymy 栏目:开发技术

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

    }

        

}















向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI