UI基础控件

一.快捷键

Home键:command + shift + h    

编译:command + B

编译和运行:command + R



二.应用界面

1.代码修改背景颜色

在ViewController中修改viewDidLoad方法

self.view.backgroundColor = [UIColor redColor];

Main.storyboard中背景颜色如果选Default就是黑色


2.工程配置介绍

General -> Identify:

Bundle Identifier:是应用的唯一标识,在应用发布的时候是必填的。

Version:版本信息。

General -> Deployment Info:

Deployment Target:就是操作系统的版本号,就像win10一样,ios也有10.0,11.2。

Device Orientation:手机运行完之后当前的屏幕状态。

General -> App Icons and launch Images:

应用图标

Info -> Custom iOS Target Properties

Bundle name:App名称


3.窗口视图控制器UI三者的关系

UIWindow在最下面,在它上面添加一个或多个视图控制器,在视图控制器上添加一个或多个UI,如UIButton,UIImageView


4.手机屏幕UIScreen

UIScreen:主要用于获取当前手机屏幕大小

640  X 960这种指的是像素尺寸,平常编程时UIScreen获取到的是物理尺寸

struct CGRect {

    CGPoint origin;

    CGSize size;

};

//获取当前手机屏幕尺寸

//bounds就是CGRect类型

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

    CGFloat height = [UIScreen mainScreen].bounds.size.height;

//源点坐标

    CGFloat x = [UIScreen mainScreen].bounds.origin.x;

    CGFloat y = [UIScreen mainScreen].bounds.origin.y;



三.UIView

1.添加一个view视图

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

view.backgroundColor = [UIColor redColor];

[self.view addSubview:view ];


2.view的Frame和Bounds属性

bounds:该view在本身坐标系统中的位置和大小

frame:该view在父view坐标系统中的位置和大小


3.view的层次结构

view添加有先后顺序,最后添加的在最上层。

添加view时,如果之前的view看不到,可能被新的覆盖,新添加的如果看不到,可能超出了屏幕范围(frame设置的不正确)


4.图层的位置交换


    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

    view1.backgroundColor = [UIColor redColor];

    [self.view addSubview:view1 ];


    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

    view2.backgroundColor = [UIColor greenColor];

    [self.view addSubview:view2 ];

    //交换

    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];



5.view的一些操作


//插入

[self.view insertSubview: view2 atIndex:1];

//把某个视图移动到最前面

[self.view bringSubviewToFront:view1];

//把某个视图移动到最后面

[self.view sendSubviewToBack:view3];



6.view子视图的快速遍历

    //view1 -> gray view2 -> 200 200 view3删除

    view1.tag = 1;

    view2.tag = 2;

    view3.tag = 3;

    NSArray *subViewArray = self.view.subviews;

    for (UIView *view in subViewArray) {

        if (view.tag == 1) {

            view.backgroundColor = [UIColor grayColor];

        }

        if (view.tag == 2) {

            view.frame = CGRectMake(200, 200, 100, 50);

        }

        if (view.tag == 3 ) {

            [view removeFromSuperview] ;

        }

    }


7.通过布局文件添加UIView

Strong & Weak

- 通过拖拽的方式添加的控件,一般都使用weak属性

- 通过代码添加的对象(控件),多数使用Strong


8.view透明度隐藏属性介绍

Alpha:1是不透明,0是全透明

整个的view视图如果修改为0,会变成黑色,说明它的底色是黑色


//通过代码修改透明度 0~1之间 0:隐藏

self.myView1.alpha = 0.5;

//隐藏的另一种方法

self.myView1.hidden = true;


9.边框阴影效果实现

    //边框

    //layer属性 在layer图层下如果使用颜色一定要用CGColor

    self.myView1.layer.borderColor = [UIColor grayColor].CGColor ;

    self.myView1.layer.borderWidth = 10;

    //圆角效果

    self.myView1.layer.cornerRadius = 10;


    //阴影

    self.myView1.layer.shadowColor = [UIColor greenColor].CGColor;

    self.myView1.layer.shadowOffset = CGSizeMake(10, 10);    //向右下的偏移量

    //透明度

    self.myView1.layer.shadowOpacity = 0.5;


9.Layer和UIView方法总览(Layer面试可能会考,很重要,答出以下四条即可)

1 layer:无法响应响应事件 UIView:可以响应

2 layer继承的是NSObject UIView继承的是UIResponder类

3 layer:内容的绘制和创建 UIView:内容的显示和管理

4 layer:实现一些特殊效果,如圆角,边框,阴影



四.UILable

1.UILable内容添加字体颜色背景色


    //1 实例化 2 属性 3 add

    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 60, 300, 50)];

    label1.backgroundColor = [UIColor grayColor];

    label1.text = @"LSY";

    label1.textColor = [UIColor blueColor];

    [self.view addSubview:label1];


2.UILable字体的使用

    //font字体

    //系统字体

    label1.font = [UIFont systemFontOfSize:12.0];

    //使用字体名称

    label2.font = [UIFont fontWithName:@"Aril-BoldMT" size:15.0];

3.UILabel的对齐方式和截断方式


    //对齐方式 text内容相对于UILabel向右对齐 有向右向左居中三种

    label3.textAlignment = NSTextAlignmentRight;

    //截断

    UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(20, 240, 300, 50)];

    label4.backgroundColor = [UIColor grayColor];

    label4.text = @"LSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSYLSY";

    label4.textColor = [UIColor blueColor];

    //font字体

    label4.font = [UIFont systemFontOfSize:16.0];

    /*

    最常用的三种截断

    NSLineBreakByTruncatingHead,    // Truncate at head of line: "...wxyz"

    NSLineBreakByTruncatingTail,    // Truncate at tail of line: "abcd..."

    NSLineBreakByTruncatingMiddle    // Truncate middle of line:  "ab...yz"

    */

    label4.lineBreakMode = NSLineBreakByTruncatingHead;

    [self.view addSubview:label4];



4.UILabel的内容调整


    //内容自适应

    //方法1

    label5.adjustsFontSizeToFitWidth = true;

    //方法2 num=0时代表当前text自适应调整并且调整的行数不固定

    label5.numberOfLines = 0;


5.UILabel的边框阴影效果

   //边框

    label6.layer.borderColor = [UIColor redColor].CGColor;

    label6.layer.borderWidth = 10.0;

    //整个label的阴影

    label6.layer.shadowColor = [UIColor greenColor].CGColor;

    label6.layer.shadowOffset = CGSizeMake(10.0, 10.0);

    label6.layer.shadowOpacity = 0.5;

    //Text内容字体阴影

    label6.shadowColor = [UIColor greenColor];

    label6.shadowOffset = CGSizeMake(2.0, 2.0 );

    [self.view addSubview:label6];



6.UILabel根据内容获取合适宽高


    UIFont *font = [UIFont systemFontOfSize:16.0];

    NSString *str = @"LSYLSY";

    //sizeWithAttributes返回合适的字符串宽高

    //参数:字典类型,用于约束当前字符串

    CGSize labelWH = [str sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];

    UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(20, 420, labelWH.width, labelWH.height)];

    label7.backgroundColor = [UIColor grayColor];

    label7.text = str;

    label7.textColor = [UIColor blueColor];

    //font字体

    label7.font = font;

    [self.view addSubview:label7];




五.UIImageView控件

1.UIImageView的认识和基本使用

UIImageView:UI + Image + View,是用来显示图片的视图

UIImage:具体的图片

步骤:

①先用UIImage把图片读取进来

②使用UIImageView将当前的UIImage显示出来

代码:


    UIImage *image1 = [UIImage imageNamed:@"150G153291P-I209.jpg"];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 300, 200) ];

    imageView.image = image1;

    [self.view addSubview:imageView ];



2.UIImageView拉伸填充效果

    //UIViewContentModeScaleToFill:铺满当前imageView      拉伸

    //UIViewContentModeScaleAspectFit:等比例拉伸

    imageView.contentMode = UIViewContentModeTop;


3.UIImageView几何变换

①中心移动

imageView.center = CGPointMake(300, 400);

②旋转

imageView.transform = CGAffineTransformMakeRotation(M_1_PI);

③缩放(x方向缩放为原来的0.5,y方向缩放为原来的0.4)

imageView.transform = CGAffineTransformMakeScale(0.5, 0.4);

④移动(向x方向偏移100,向y方向偏移80)

imageView.transform = CGAffineTransformMakeTranslation(100, 80);


4.UIImageView幻灯片功能

    NSMutableArray *imageArray = [[NSMutableArray alloc]init];

    UIImage *image1 = [UIImage imageNamed:@"150G153291P-I209.jpg"];

    UIImage *image2 = [UIImage imageNamed:@"银雪壁纸电脑版1.jpg"];

    UIImage *image3 = [UIImage imageNamed:@"银雪壁纸电脑版2.jpg"];

    UIImage *image4 = [UIImage imageNamed:@"银雪壁纸手机版1.jpg"];

    [imageArray addObject:image1];

    [imageArray addObject:image2];

    [imageArray addObject:image3];

    [imageArray addObject:image4];


    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 300, 200)];

    imageView.animationDuration = [imageArray count];  //执行周期,本例当中一张图片一秒

    imageView.animationImages = imageArray;    //需要播放的图片

    imageView.animationRepeatCount = 2;    //重复次数

    [self.view addSubview:imageView ];

    [imageView startAnimating];



5.一些知识点补充

    //阴影

    imageView.layer.shadowColor = [UIColor greenColor].CGColor;

    imageView.layer.shadowOffset = CGSizeMake(5, 5);

    imageView.layer.shadowOpacity = 0.5;

    //透明度

    imageView.alpha = 0.5;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容