zoukankan      html  css  js  c++  java
  • 继承UIView的初始化 、重绘、以及绘制图片

    大家对于UIViewController的生命周期都相当了解了。但是对于继承UIView的子类能做什么,却很少有文章介绍的。

    1.  -initWithFrame:(CGRect)rectview指定的初始化方法。如果要继承UIView 的初始化就需要直接或间接的调用这个方法。

    具体使用如下:

    - (instancetype)initWithFrame:(CGRect)frame{
       
     self = [super initWithFrame:frame];
       
     if (self) {
            [self
     createUI];
        }
       
     return self;

    }

    注意到在初始化中,做了一些额外的事情 [self createUI];

    2. 当你需要调用-(void)layoutSubViews:来布局子视图。一般视图需要重绘时需要会调用layoutSubViews.

    layoutSubView都是什么时候会被调用:

    • init 时,并不会调用layoutSubviews

    • [A addSubView:B]会导致A 和B 以及其所有的subviews的layoutSubviews被调用。

    •  view 的setFrame会智能的调用layoutSubviews。会先判断view 的frame是否发生改变,如果改变了就调用layoutSubViews,否则将不会调用。

    • scrolling a UIScrollView causes layoutSubviews to be called on the scrollView, and its superview  滚动一个UIScrollView会导致在scrollView和它之上的superView的layoutSubViews会被调用。

    • 旋转一个设备仅仅会调用viewController对应的父 view 上会调用layoutSubView

    • Resizing a view will call layoutSubviews on its superview

    重新调整一个view的时候会调用它的superview的layoutSubViews方法

     

    - (void)layoutSubviews{

        [super layoutSubviews];

    // 背景
        [_backImageView
     setFrame:self.bounds];

        CGRect rectTitle = CGRectMake(00self.width_titleLabel.font.pointSize);

        [_titleLabel setFrame:rectTitle];
        [_textLabel
     setFrame:rectText];

    }

     

    注意一定要调用[super layoutSubViews]方法。

    3.  - (void)drawRect:(CGRect)rect 用传递给的Rect给接受者 绘制图片

    用在哪里:  如果执行平常的绘图操作,就需要用到这个方法。

     

    1. // Only override drawRect: if you perform custom drawing.  
    2. // An empty implementation adversely affects performance during animation.  
    3. - (void)drawRect:(CGRect)rect  
    4. {  
    5.     UIColor *color = [UIColor redColor];  
    6.     [color set]; //设置线条颜色  
    7.       //创建
    8.     UIBezierPath* aPath = [UIBezierPath bezierPath];
    9. //线的属性 设置 
    10.     aPath.lineWidth = 5.0;  
    11.     aPath.lineCapStyle = kCGLineCapRound; //线条拐角  
    12.     aPath.lineJoinStyle = kCGLineCapRound; //终点处理  
    13.         
    14.     // Set the starting point of the shape.  
    15.     [aPath moveToPoint:CGPointMake(100.0, 0.0)];  
    16.         
    17.     // Draw the lines  
    18.     [aPath addLineToPoint:CGPointMake(200.0, 40.0)];  
    19.     [aPath addLineToPoint:CGPointMake(160, 140)];  
    20.     [aPath addLineToPoint:CGPointMake(40.0, 140)];  
    21.     [aPath addLineToPoint:CGPointMake(0.0, 40.0)];  
    22.     [aPath closePath];//第五条线通过调用closePath方法得到的  
    23.         
    24.     [aPath stroke];//Draws line 根据坐标点连线  

    何时会被调用:

         当显式的调用[self setNeedDisplay]时会在下个event loop 被调用。或在viewController里调用[self.view setNeedDisplay]也可以。

    4. 在重写tabelView cell 时会用到 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    使用方法如下:

    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

            //进行额外的操作

            [self creatUI];
        }
       
     return self;

    }

    当然操作的时候要注意尽量在cell content view上进行操作。

    tableview cell 里也可以调用UIView指定的初始化方法。

     

     

     

  • 相关阅读:
    SDL 学习及相关API
    ppm图像格式
    Gstreamer学习
    GObject对象系统
    Linux下查看文件和文件夹大小
    将输入的字符串按指定的长度进行拆分
    Ubuntu12.04 下安装Chrome浏览器
    Ubuntu12.04 下搭建Java开发环境
    Android 之 WebView
    Ubuntu Desktop 16.04 LTS 下成功配置Jupyter的两个python内核版本(2.7x,3.5x)
  • 原文地址:https://www.cnblogs.com/ljlkfx/p/4845830.html
Copyright © 2011-2022 走看看