Fork me on GitHub

UI- 基础控件零散知识点回归

    1.frame\center\bounds

     1> frame:能修改位置和尺寸

     2> center:能修改位置

     3> bounds:能修改尺寸(x\y一般都是0)

     

    2.在.m中声明的方法是私有方法,外界无法直接访问,保证了封装性

     

     3.UIView 一般翻译叫做:视图\控件\组件

     UIButton、UILabel、UITextField都继承自UIView

     3.1.UIImageView和UIButton

     1> 使用场合

     * UIImageView: 如果仅仅是显示图片,不需要监听图片的点击

     * UIButton: 既要显示图片,又要监听图片的点击

     2> 相同:能显示图片

     3> 不同点

     * UIButton能处理点击事件, UIImageView不能处理点击事件

     * UIButton既能显示图片, 又能显示文字

     * UIButton能同时显示两张图片

     * UIButton继承自UIControl, 因此默认就能处理事件  UIButton --> :UIControl -->:UIView

     * UIImageView继承自UIView, 因此默认就不能处理事件 UIImageView --> :UIView

     4> 如何选择

     UIButton:需要显示图片,点击图片后需要做一些特定的操作

     UIImageView:仅仅需要显示图片,点击图片后不需要做任何事情

     

     4.键盘的显示与隐藏

     resignFirstResponder

     当叫出键盘的那个控件(第一响应者)调用这个方法时,就能退出键盘

     endEditing

     只要调用这个方法的控件内部存在第一响应者,就能退出键盘

     

     5.常用按钮的状态

     normal(普通状态)

     默认情况(Default)

     对应的枚举常量:UIControlStateNormal

     

     highlighted(高亮状态)

     按钮被按下去的时候(手指还未松开)

     对应的枚举常量:UIControlStateHighlighted

     

     disabled(失效状态,不可用状态)

     如果enabled属性为NO,就是处于disable状态,代表按钮不可以被点击

     对应的枚举常量:UIControlStateDisabled

     

     6.一个UIColor代表一种颜色,通过UIColor的类方法,可以获得很多常用的颜色

     + (UIColor *)blackColor;      // 0.0 white 黑色

     + (UIColor *)darkGrayColor;   // 0.333 white 深灰色

     + (UIColor *)lightGrayColor;  // 0.667 white 亮灰色

     + (UIColor *)whiteColor;      // 1.0 white 白色

     + (UIColor *)grayColor;       // 0.5 white 灰色

     + (UIColor *)redColor;        // 1.0, 0.0, 0.0 RGB 红色

     + (UIColor *)greenColor;      // 0.0, 1.0, 0.0 RGB 绿色

     + (UIColor *)blueColor;       // 0.0, 0.0, 1.0 RGB 蓝色

     + (UIColor *)cyanColor;       // 0.0, 1.0, 1.0 RGB 青色

     + (UIColor *)yellowColor;     // 1.0, 1.0, 0.0 RGB 黄色

     + (UIColor *)magentaColor;    // 1.0, 0.0, 1.0 RGB 品红

     + (UIColor *)orangeColor;     // 1.0, 0.5, 0.0 RGB 橙色

     + (UIColor *)purpleColor;     // 0.5, 0.0, 0.5 RGB 紫色

     + (UIColor *)brownColor;      // 0.6, 0.4, 0.2 RGB 棕色

     + (UIColor *)clearColor;      // 0.0 white, 0.0 alpha 清除颜色(空色)

     

    7. 一个UIImage对象代表一张图片,一般通过imageNamed:方法就可以通过文件名加载项目中的图片

     (文件名可以省略扩展名)

     UIImage *image = [UIImage imageNamed:@"btn_01"];

     

    8. NSBundle

     1> 一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹

     2> 利用mainBundle就可以访问软件资源包中的任何资源

     

    9. 将属性放在get方法中初始化的方式,称为“懒加载”\”延迟加载”

     

    10. UIImageView帧动画相关属性和方法

     @property(nonatomic,copy) NSArray *animationImages;

     需要播放的序列帧图片数组(里面都是UIImage对象,会按顺序显示里面的图片)

     @property(nonatomic) NSTimeInterval animationDuration;

     帧动画的持续时间

     @property(nonatomic) NSInteger animationRepeatCount;

     帧动画的执行次数(默认是无限循环)

     - (void)startAnimating;

     开始执行帧动画

     - (void)stopAnimating;

     停止执行帧动画

     - (BOOL)isAnimating;

     是否正在执行帧动画

     

     11. UIImage的2种加载方式

     方式一:有缓存(图片所占用的内存会一直停留在程序中)

     + (UIImage *)imageNamed:(NSString *)name;

     name是图片的文件名

     

     方式二:无缓存(图片所占用的内存会在一些特定操作后被清除)

     + (UIImage *)imageWithContentsOfFile:(NSString *)path

     - (id)initWithContentsOfFile:(NSString *)path;

     path是图片的全路径

  例如:

  1.1 定义 UIImageView

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    

    1.2 定义 NSMutableArray 保存所有图片

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

    int num = 40;

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

        图片的名称

        NSString *imgName = [NSString stringWithFormat:@"%@_%02d.jpg", imgView, i ];

        在创建比较小的,并且常用的图片时,可以使用imageNamed:方法创建

        UIImage *img = [UIImage imageNamed:imgName];

        

        如果创建的图片很多,并且比较大的话,应该使用这种方式创建

        NSString *filePath = [[NSBundle mainBundle] pathForResource:imgName ofType:nil];

        UIImage *img = [[UIImage alloc]initWithContentsOfFile:filePath];

        把图片添加到数组中

        [animationImgsArrM addObject:img];

    }

     

     12. 用模型取代字典的好处

     1> 使用字典的坏处:

     一般情况下,设置数据和取出数据都使用“字符串类型的key”,编写这些key时,编译器不会有任何友善提示,需要手敲

     dict[@"name"] = @"Jack";

     NSString *name = dict[@"name"];

     手敲字符串key,key容易写错

     Key如果写错了,编译器不会有任何警告和报错,造成设错数据或者取错数据

     2> 使用模型的好处:

     所谓模型,其实就是数据模型,专门用来存放数据的对象,用它来表示数据会更加专业

     模型设置数据和取出数据都是通过它的属性,属性名如果写错了,编译器会马上报错,因此,保证了数据的正确性

     使用模型访问属性时,编译器会提供一系列的提示,提高编码效率

     app.name = @"Jack”;

     NSString *name = app.name;

     

     13. instancetype

     instancetype在类型表示上,跟id一样,可以表示任何对象类型

     instancetype只能用在返回值类型上,不能像id一样用在参数类型上

     instancetype比id多一个好处:编译器会检测instancetype的真实类型

     

     14. Xib文件可以用来描述某一块局部的UI界面

     Xib文件的加载

     方法1

     NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"AppView" owner:nil options:nil];

     这个方法会创建xib中的所有对象,并且将对象按顺序放到objs数组中

     方法2

     bundle参数可以为nil,默认就是main bundle

     UINib *nib = [UINib nibWithNibName:@"MJAppView" bundle:[NSBundle mainBundle]];

     NSArray *objs = [nib instantiateWithOwner:nil options:nil];

     在开发阶段,面向开发者的是xib文件; 当把应用装到手机上时,xib文件就会转为nib文件

 

     15. Xib和storyboard对比

     1> 共同点:

     都用来描述软件界面

     都用Interface Builder工具来编辑

     2> 不同点

     Xib是轻量级的,用来描述局部的UI界面

     Storyboard是重量级的,用来描述整个软件的多个界面,并且能展示多个界面之间的跳转关系

     

     16. UILabel的常见设置

     @property(nonatomic,copy)   NSString           *text;

     显示的文字

     @property(nonatomic,retain) UIFont             *font;

     字体

     @property(nonatomic,retain) UIColor            *textColor;

     文字颜色

     @property(nonatomic)        NSTextAlignment    textAlignment;

     对齐模式(比如左对齐、居中对齐、右对齐)

     

     17. UIFont --> UIFont代表字体,常见创建方法有以下几个:

     + (UIFont *)systemFontOfSize:(CGFloat)fontSize;   系统默认字体

     + (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize;  粗体

     + (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize;  斜体

     

     18. UIButton的常见设置

     - (void)setTitle:(NSString *)title forState:(UIControlState)state;

     设置按钮的文字

     - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;

     设置按钮的文字颜色

     - (void)setImage:(UIImage *)image forState:(UIControlState)state;

     设置按钮内部的小图片

     - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;

     设置按钮的背景图片

     btn.titleLabel.font = [UIFont systemFontOfSize:13];

     设置按钮的文字字体(需要拿到按钮内部的label来设置)

     - (NSString *)titleForState:(UIControlState)state;

     获得按钮的文字

     - (UIColor *)titleColorForState:(UIControlState)state;

     获得按钮的文字颜色

     - (UIImage *)imageForState:(UIControlState)state;

     获得按钮内部的小图片

     - (UIImage *)backgroundImageForState:(UIControlState)state;

     获得按钮的背景图片

     

     19.使用xib封装一个自定义view的步骤

     1> 新建一个继承UIView的自定义view,假设类名叫做(AppView)

     2> 新建一个MJAppView.xib文件来描述MJAppView内部的结构

     3> 修改UIView的类型为MJAppView真是类型

     4> 将内部的子控件跟MJAppView进行属性连线

     5> MJAppView提供一个模型属性

     6> 重写模型属性的set方法,因为在set方法中可以拿到外界传递的模型数据

     7> 把模型数据拆开,分别设置数据到对应的子控件中

     8> 提供一个创建MJAppView的类方法,将读取xib文件的代码屏蔽起来

  

  20. 清屏

  [[self.view subviews]makeObjectsPerformSelector:@selector(removeFromSuperview)];

  

posted @ 2016-04-10 11:03  极度恐慌_JG  阅读(180)  评论(0编辑  收藏  举报