UI期的基础类 UIView 基本运行顺序

1 篇文章 0 订阅

UI期的基础类  UIView

1.视图的位置和大小的确定,需要在视图初始化的时候(initWithFrame)需要传递一个参数,此参数为结构体(CGRect)其实就是一个矩形,该矩形分为两部分,一个用来确定位置(origin.x&origin.y)一个用来确定大小(size.width&size.height).每一个视图的frame都是以该视图父视图的左上角为(0,0)点。


2.背景颜色的设置:每一个视图及其子类都会有一个属性backgroundColor,参数为UIColor类型。

3.将初始化好的视图呈现在父视图上面:[父视图  addSubview:子视图];   addSubview调用越早,层级越是在最底层。


创建应用程序主窗口,一般一个应用程序只会用一个主窗口 UIWindow;

frame:视图的大小;[UIScreen mainScreen].bounds 整个屏幕的大小;


 //初始化时,将window的大小设置为整个屏幕的大小;

 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];


 //将window设置为白色,如果不设置为透明; 类方法

self.window.backgroundColor = [UIColor whiteColor];


//将初始化好的window设置为主window并让其显示;

    [self.window makeKeyAndVisible];


[self.window setRootViewController:[[UIViewController alloc]init]];

&&

UIViewController *VC = [[UIViewController alloc] init];

self.window.rootViewController = VC;



   //初始化一个视图 (UIView)

    //设定视图的位置和大小 CGRect 为一个结构体,里面规定矩形的初始位置(x和y点)宽和高      ( weight和width)

//    原点左上角,横为X 竖为Y

   CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)

    CGRect frame = CGRectMake(50, 50, 100, 100);

    UIView *myView = [[UIView alloc] initWithFrame:frame];


//iOS 7 扁平化之后,基本上所有视图都为透明色,所以要设置背景;

   [myView setBackgroundColor:[UIColor yellowColor]];


//将初始化之后的视图呈现在window上;

  [self.window addSubview:myView1];


 //初始化视图,将该视图添加到 myView 上;

    UIView *subOfMyView = [[UIView alloc] initWithFrame:CGRectMake(60, 0, 50, 50)];

    [subOfMyView setBackgroundColor:[UIColor redColor]];

//    frame 的原点参考的坐标系是以父视图的左上角为(0,0);

    [myView addSubview:subOfMyView];





坐标获取

CGFloat width = myView1.frame.size.width;

    CGFloat X = myView1.frame.origin.x;

    CGFloat screenWidth = self.window.frame.size.width;

    CGFloat screenHight = self.window.frame.size.height;

屏幕

     float   height = [UIScreen mainScreen].bounds.size.herght;

     float   width  = [UIScreen  mainScreen].bounds.size.width;



UIView常用属性 


//改变一下bounds的值.frame参照的是父视图的坐标系,意思就是说,frame是myView在父视图上的位置表现,bounds 参照的是本身,bounds改变后影响的是子视图;

      //获得myView的frame,并将其转化成字符串类型;

     NSString *frame = NSStringFromCGRect(myView.frame);

    NSString *frame1 = NSStringFromCGRect(myView.bounds);

    myView.frame = {{50, 50}, {80, 200}}, 

     myView.bounds = {{0, 0}, {80, 200}}


 //视图的中心点   参考系为父视图

     NSString *point1 = NSStringFromCGPoint(myView.center);


点语法注意项

//     .center 是OC语言下的调用属性方法的点语法;

//    point.x式结构体的点语法,也就是C语言中的点语法,意义不同,不可通用;




main.m作用

//配置应用的一个main函数;

//        argc 与 argv 前两个参数是应用启动所需要的一些必要数据不需要我们改动;系统自动配置好;

//        第三个参数:将应用程序类名转换为字符串作为该函数的参数,如果赋值为nil,意思为值为@“UIAppplication”,这个参数可以是UIApplication的子类。通过这个参数

//        第四个参数:创建了应用程序的代理

//        当应用程序对象创建完成之后,将二者建立关联,也就是应用程序将代理指定为整个应用的的代理,来协助应用程序处理逻辑操作;

//        当应用程序和代理对象建立关联后,会将整个应用程序放入RunLoop中 (为一死循环)只要我们不退出应用,这个循环就会一直循环。状态:等待操作——>处理操作——>处理操作——>等待操作······


 //此函数在内部根据字符串创建该字符串类型的对象所需的方法,将字符串转化成类;

//        Class class = NSClassFromString(@“AppDelegatr");

         又类确定一个对象;

          id testApp = [[class alloc] init];


main执行结束后

//当main函数执行完,应用程序创建完毕,应用的代理也指定为当前类的对象,也将应用放到RunLoop中了,这个时候,整个应用程序才算加载完毕,完毕后就会调用此代理方法,再次代理方法中设置页面;


  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  •     return YES;
  • }



//应用将要取消活跃状态,正在看视频时有电话;视频APP将取消活跃状态;将调用一下方法;

- (void)applicationWillResignActive:(UIApplication *)application {

    NSLog(@"消活跃状态 = %s",__func__);

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


//程序已经进入后台,一般我们将一些重要数据保存,因为苹果手机应用进入后台之后,长时间不用,应用就会推出后台;

- (void)applicationDidEnterBackground:(UIApplication *)application {

    NSLog(@"进入后台 = %s",__func__);

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

//在后台 当应用程序从新进入前端时,只执行此代理;

- (void)applicationWillEnterForeground:(UIApplication *)application {

    NSLog(@"进入前端 = %s", __func__);

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


//当应用程序进入前端时,暂停的一些操作从新启动;

- (void)applicationDidBecomeActive:(UIApplication *)application {

    NSLog(@"重新活跃 = %s", __func__);

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


//当应用程序进入前端时,暂停的一些操作从新启动;

- (void)applicationDidBecomeActive:(UIApplication *)application {

    NSLog(@"重新活跃 = %s", __func__);

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


宏定义

//三目

#define Max(A, B)  ((A)>(B)?(A):(B))

//三原色

#define RGBA(r, g, b, a)  [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]

//字符串提取

#define STRINGCUT(s, number)  [s substringToIndex:(number)]

















































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值