UI基础(一)

来源:互联网 发布:js中name选择器 编辑:程序博客网 时间:2024/06/11 21:44

UI基础(一)

一、Window就是一个窗口,通过这个窗口,可以看到窗口里面的内容,在iOS里面,一般只有一个窗口,用于管理、协调应用中显示的控件

主要作用:

1、它是个容器,给 view(视图)提供展示的区域。

2、将事件(如:旋转、点击等)分发给视图。

1)UIWindow其实也是一个view(视图),它也继承自UIView

2)UIWindow 一般和硬件的尺寸一样大。

3)UIWindow 中有一个非常重要的属性:rootViewController(用来告诉系统,哪一个是根视图控制器)。

视图控制器:用来调度管理视图的控制者。

4)还有一个非常重要的方法:makeKeyAndVisible(让窗口显示并且接收事件)。


二、UIView详解

UIView:UIView是视图,代表屏幕上的一个矩形区域。各种UI控件都属于View,不同的控件属于不同种类的视图,iOS中所有能看到的内容都是UIView或其子类。

1、只要是视图控件,都需要有自己的位置

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

注:

1)Frame是坐标体系(位置)里面的一种,它是相对父视图或者硬件设备来获取自己的位置的(它需要一个参考物,来得到自己的位置。

2)bounds是坐标体系(位置)里面的一种,相对自身来说,也就是以自己为主,自己的边境范围。

    

2、UIColor:所有颜色的类

self.window.backgroundColor = [UIColor brownColor];

    

UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(100

, 100, 100, 150)];


3CGRect

struct CGRect {

     CGPoint origin; origin:是一个点(原点)起始点

     CGSize size;    size:自身的大小尺寸

};

注:    

1)Frame需要一个结构体(CGRect)

2)Frame是依据他的父视图来确定自己所在的位置的。

    

4、backgroundColor背景色

view1.backgroundColor = [UIColor whiteColor];


5、alpha:透明度 

1)alpha视图透明度的属性默认值是1(也就是完全不透明)。

2)alpha透明度是0的时候,就不会再接收任何触发事件。

view1.alpha = 0.2;


6、tag

tag:类似于身份证号码可以通过tag来找到对应的视图(依赖于父视图的,在它的父视图上,通过tag找到这个视图)。

view1.tag = 100;

    

7、bounds的原点永远都是(0,0

view1.bounds = CGRectMake(0, 0, 200, 200);

    

8、hidden默认值是NO,控制视图是否隐藏,如果是YES就隐藏视图,如果是NO就不隐藏视图。

view1.hidden = NO;


9、center中心点

相对于它的父视图来说的 (如果改变中心点的位置,视图的位置也会发生改变)。

view.center = CGPointMake(200, 300);

    

10、将视图的中心点移到父视图的中心点

方法一:

view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);

方法二:

view.center = CGPointMake(CGRectGetWidth([UIScreen mainScreen].bounds)/2, CGRectGetHeight([UIScreen mainScreen].bounds)/2);

注:

CGRectGetWidth:获取宽的函数,获取的是括号中的对象的宽

CGRectGetHeight:获取高的函数,获取的是括号中的对象的高

方法三:

view.center = self.window.center;


    

11、addSubviewview1贴到窗口上(父视图上)

[self.window addSubview:view1];


12、makeKeyAndVisible:让窗口显示并接收事件

[self.window makeKeyAndVisible];


13、定时器的写法:

写法一:

NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(changeColor) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];   

写法二:

[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeColor) userInfo:nil repeats:YES];


14、打印设备的bounds

1)[UIScreen mainScreen].bounds得到设备尺寸的大小

NSLog(@"宽:%f, 高:%f", [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);


15、viewWithTag通过这个方法,可以找到对应tag的视图,在它的父视图上查找。

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


例:霓虹灯练习

#import "AppDelegate.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



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

    

    colorList = @[[UIColor whiteColor], [UIColor blueColor], [UIColor blackColor], [UIColor yellowColor], [UIColor grayColor], [UIColor greenColor], [UIColor orangeColor], [UIColor darkGrayColor], [UIColor lightGrayColor], [UIColor cyanColor], [UIColor redColor], [UIColor purpleColor], [UIColor brownColor]];

    

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

//    self.window.backgroundColor = [UIColor brownColor];

    

    for (int i = 0; i < 19; i ++) {

        int theHeight = arc4random()%560;

        int num = arc4random()%colorList.count;

        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20 * i, 0, 20, theHeight + 100)];

        view.backgroundColor = colorList[num];

        view.tag = 100 + i;

        [self.window addSubview:view];

    }

    

    [self.window makeKeyAndVisible];

    

    [NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:@selector(changeColor) userInfo:nil repeats:YES];


    

    NSLog(@"宽:%f, 高:%f", [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

    

    

    return YES;

}


- (void)changeColor{

    for (int i = 0; i < 19; i ++) {

        int theHeight = arc4random()%560;

        int num = arc4random()%colorList.count;

        UIView *theView = [self.window viewWithTag:100 + i];

        theView.backgroundColor = colorList[num];

        theView.frame  = CGRectMake((20 + 2) * i, 0, 20, theHeight + 100);

    }

}




0 0