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

1.  -initWithFrame:(CGRect)rect是view指定的初始化方法。如果要继承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(0, 0, self.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指定的初始化方法。

 

 

 

继承UIView的初始化 、重绘、以及绘制图片的更多相关文章

  1. 关于UIView及其子类重绘drawRect

    转载自:https://nezha.gitbooks.io/ios-developmentarticles/content/UIView%E7%9A%84drawRect%E9%87%8D%E7%BB ...

  2. iOS之 重绘机制

    最近在看Core Animation , 今天来谈谈CALayer 和 UIView 中的重绘的一些认识: 我们都知道UIView里面有个成员layer,利用这个这个layer我们可以设置一些圆角,阴 ...

  3. swing容器继承重绘问题解决

    swing容器继承重绘问题解决   以JPanel为例,继承JPanel,想动态为器更换背景,这就涉及到重绘问题.一下是本人重写代码: package ui; import java.awt.Grap ...

  4. 关于echarts绘制树图形的注意事项(文字倾斜、数据更新、缓存重绘问题等)

    最近项目中使用到echarts的树操作,对其中几点注意事项进行下总结. 效果图: 1.基础配置 options的配置如下: { tooltip: { trigger: 'item', triggerO ...

  5. 窗体背景的绘制(Windows窗体每次都会重绘其窗体背景,所以我们可以通过拦截窗体重绘背景的消息(WM_ERASEBKGND),并自定义方法来实现重绘窗体背景)

    核心思想:由于Windows窗体每次都会重绘其窗体背景,所以我们可以通过拦截窗体重绘背景的消息(WM_ERASEBKGND),并自定义方法来实现重绘窗体背景.通过TImage组件也可以实现,但是重写W ...

  6. iOS 视图:重绘与UIScrollView(内容根据iOS编程编写)

    我们继续之前的 Hypnosister 应用,当用户开始触摸的时候,圆形的颜色会改变. 首先,在 JXHypnosisView 头文件中声明一个属性,用来表示圆形的颜色. #import " ...

  7. iOS之UI--Quartz2D的入门应用--重绘下载圆形进度条

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  8. 重绘TabControl

    本文转载自:http://blog.csdn.net/conmajia/article/details/7596718 作者:野比 (conmajia@gmail.com) 时间:May, 2012 ...

  9. UITextField的常用属性,Delegate,重绘

        一  属性 UITextField * myTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 50 ...

随机推荐

  1. calicoctl命令简介

    背景 在calico中,有多种网络资源.以v1.6.1为例,网络资源包含:node,bgpPeer,hostEndpoint,workloadEndpoint,ipPool,policy,profil ...

  2. Deques and Randomized Queues

    1. 题目重述 完成三个程序,分别是双向队列,随机队列,和随机队列读取文本并输出k个数. 2. 分析 2.1 双向队列 题目的性能要求是,操作时间O(1),内存占用最大48n+192byte. 当使用 ...

  3. Asset Catalog Help (四)---Adding an iOS App Icon Set or Launch Image Set

    Adding an iOS App Icon Set or Launch Image Set Organize different resolutions of your app icons and ...

  4. c/c++面试12-18------关与sizeof那些事儿

    12 使用sizeof计算普通变量所占空间大小 (1)不同数据类型所占字节数不同(32位 64位系统不同) int----->4 double----->8 char-------> ...

  5. HDU - 4006 The kth great number multiset应用(找第k大值)

    The kth great number Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming ...

  6. LeetCode: 500 Keyboard Row (easy)

    题目: Given a List of words, return the words that can be typed using letters of alphabet on only one ...

  7. C#中的explicit和implicit了解一下吧

    今天在研究公司项目框架的时候看到了下面的用法,public static implicit operator JsonData(int data);.貌似很久没用过这种隐式转换的写法了,因此重新温习一 ...

  8. E20190303-hm

    invoke vt. 乞灵,祈求; 提出或授引…以支持或证明; 召鬼; 借助;

  9. 使用AnimatorOverrideController动态更换animationclip注意事项

    http://www.ceeger.com/forum/read.php?tid=19138 public AnimationClip clip; Animator anim; void Awake( ...

  10. [Xcode 实际操作]二、视图与手势-(5)给图像视图添加圆角效果

    目录:[Swift]Xcode实际操作 本文将演示给矩形图片添加圆角效果 import UIKit class ViewController: UIViewController { override ...