首页 > 其他 > 详细

继承UIView的初始化 、重绘、以及绘制图片

时间:2015-09-29 12:50:22      阅读:104      评论:0      收藏:0      [点我收藏+]

大家对于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(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?根据坐标点连线??
  25. }?

何时会被调用:

? ? ?当显式的调用[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的初始化 、重绘、以及绘制图片

原文:http://www.cnblogs.com/ljlkfx/p/4845830.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!