继承UIView的初始化 、重绘、以及绘制图片
大家对于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给接受者
绘制图片
用在哪里: 如果执行平常的绘图操作,就需要用到这个方法。
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect
- {
- UIColor *color = [UIColor redColor];
- [color set]; //设置线条颜色
- //创建
- UIBezierPath* aPath = [UIBezierPath bezierPath];
- //线的属性 设置
- aPath.lineWidth = 5.0;
- aPath.lineCapStyle = kCGLineCapRound; //线条拐角
- aPath.lineJoinStyle = kCGLineCapRound; //终点处理
- // Set the starting point of the shape.
- [aPath moveToPoint:CGPointMake(100.0, 0.0)];
- // Draw the lines
- [aPath addLineToPoint:CGPointMake(200.0, 40.0)];
- [aPath addLineToPoint:CGPointMake(160, 140)];
- [aPath addLineToPoint:CGPointMake(40.0, 140)];
- [aPath addLineToPoint:CGPointMake(0.0, 40.0)];
- [aPath closePath];//第五条线通过调用closePath方法得到的
- [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指定的初始化方法。
最新文章
- 适配iOS 10以及Xcode 8(转载)
- Access批量操作
- MySQL Batch 与 Transaction
- grunt安装
- stl-基本知识
- java集合框架之java HashMap代码解析
- 流行的MySql版本
- LeetCode OJ 160. Intersection of Two Linked Lists
- JAVA基础:自己构造一个按递增排列的数组,用户输入一个数,插入适当位置
- Android使用百度地图定位并显示手机位置后使用前置摄像头“偷拍”
- linux系统的磁盘空间限制的两种方法
- java碎笔
- Aho-Corasick算法学习
- html与ios交互方法 WebViewJavascriptBridge
- Vue.js——vue-resource
- 终极版:Mybatis整合Spring配置
- localStorage和sessionStorage数据存储
- Docker:网络及数据卷设置 [四]
- linx6.7 update openssh to 7.7p1
- 神州数码OSPF路由汇总配置
热门文章
- <;正则吃饺子>; :关于前端往后端传递布尔值参数的问题
- 爬虫库之BeautifulSoup学习(四)
- WordCount作业提交到FileInputFormat类中split切分算法和host选择算法过程源码分析
- zz 堆空间与栈空间
- python3 封装之property 多态 绑定方法classmethod 与 非绑定方法 staticmethod
- jquery操作select(option)的取值,设置和选中
- SQL Server服务器连接配置
- 201621123016 《Java程序设计》第十一周学习总结
- 两个MATLAB在线工具,画图啥的都不用安装了
- HDU2594 【KMP】