UIView的那几个混淆方法

- 首先你进入任意一个工程 ,然后:command+shift+o ,搜索 UIView。

嗯,好多属性和方法。一眼扫过去,其实很多都会。

开发中经常会使用到自定义View。那么下面的几个方法你会了吗?
好,我们来看看这几个比较容易混淆的方法:

  • (instancetype)initWithFrame:(CGRect)frame

1、 你经常是这样写:

- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];// 先调用父类的initWithFrame方法
if (self) {
    
    // 再自定义该类(UIView子类)的初始化操作。
    _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    [_imageView setFrame:CGRectMake(0, 0, 200, 200)];
    _imageView.contentSize = CGSizeMake(320*5, 200);
    
    [self addSubview:_imageView];
}
  return self;
}

以上的那样是OK的,没出现问题,因为,你在初始化的时候就明确了具体的大小了。
如果你使用init 方法,那么结果是什么呢?
答:答案是啥也没有!
- (instancetype)init
{
self = [super init];
if (self) {
// 前提是使用了,frame 或bounds 不是具体的尺寸
}
return self;
}
如果要是这样:把数字替换成当前的视图的frame 或bounds的话,那就会出现莫名其妙的事了,因为可能没有值:
- (instancetype)initWithFrame:(CGRect)frame {

if (self = [super initWithFrame:frame]) {
   self.imageView = [[UIImageView alloc]init];
   [self addSubview:self.imageView];
   self.label = [[UILabel alloc]init]; 
   self.label.textAlignment = NSTextAlignmentCenter; 
   [self addSubview:self.label]; 
  } 
    return self;
}  
2、 嗯,解决这个问题,那就要使用 layoutSubviews 了。
- (void)layoutSubviews { 
  [super layoutSubviews];
  // 这里拿到的大小才是真实的
  CGFloat width = self.bounds.size.width; 
  CGFloat height = self.bounds.size.height; 
  self.imageView.frame = CGRectMake(0, 0, width, width);
  self.label.frame = CGRectMake(0, width, width, height - width);
}
2、1 稍微扩展一下:frame 和 bounds 。frame是针对它的父视图的,bounds 是针对它本身的大小。 说白了就是:参照物不同(一个是参照父视图,一个是参照自己)。

给你看看一张经典的图:

frame和bounds.png

1、bounds的原点是(0,0)点(就是view本身的坐标系统,默认永远都是0,0点,除非认为setbounds)
2、而frame的原点却是任意的(相对于父视图中的坐标位置)。

//所以:以后如果你想获得某个view 或视图的大小,就可以使用:
CGFloat width = self.bounds.size.width; 
CGFloat height = self.bounds.size.height;

总结: 养成好习惯,在initWithFrame 里设置想要的控件。在layoutSubviews 设置控件的大小。

3、另外一种自定义控件的方法就是采用懒加载方式。在重写get方法里直接设置大小也是OK的。

比如说:我这个就不用layoutSubviews 来设置大小了。

懒加载自定义View.png
4、哪 layoutSubviews 又是什么,怎么使用?

答:layoutSubviews 是刷新子对象布局。
苹果是这样子说的:
You should override this method only if the autoresizing behaviors of the subviews do not offer the behavior you want.layoutSubviews.
意思是:只要你的子视图尺寸发生改变或调整尺寸时,那么就要重写该方法。

1、init初始化不会触发layoutSubviews, 但是是用initWithFrame 进行初始化时,当rect的值不为CGRectZero时,也会触发
2、addSubview会触发layoutSubviews
3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化
4、滚动一个UIScrollView会触发layoutSubviews
5、旋转Screen会触发父UIView上的layoutSubviews事件
6、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件

  • (void)setNeedsLayout;
    需要重新布局,异步调用layoutIfNeeded刷新布局,不立即刷新,但layoutSubviews一定会被调用。

  • (void)layoutIfNeeded;
    // override point. called by layoutIfNeeded automatically. As of iOS 6.0,
    when constraints-based layout is used the base implementation applies the constraints-based layout, otherwise it does nothing.

  • (void)layoutSubviews;

这个方法,默认没有做任何事情,需要子类进行重写

5、iOS重绘机制drawRect

- (void)drawRect:(CGRect)rect;
  重写 此方法,执行重绘任务

  // default is UIViewContentModeScaleToFill
  @property(nonatomic) UIViewContentMode contentMode;              
  - (void)drawRect:(CGRect)rect { 
   // Drawing code.  
  //获得处理的上下文     
   CGContextRef context = UIGraphicsGetCurrentContext();       
   //设置线条样式     
   CGContextSetLineCap(context, kCGLineCapSquare);    
  //设置线条粗细宽度      
  CGContextSetLineWidth(context, 1.0); 
  //设置颜色    
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);    
   //开始一个起始路径       
   CGContextBeginPath(context);     
    //起始点设置为(0,0):注意这是上下文对应区域中的相对坐标,      
    CGContextMoveToPoint(context, 0, 0);   
    //设置下一个坐标点     
    CGContextAddLineToPoint(context, 100, 100);         
    //设置下一个坐标点    
    CGContextAddLineToPoint(context, 0, 150); 
     //设置下一个坐标点  
    CGContextAddLineToPoint(context, 50, 180);
    //连接上面定义的坐标点
    CGContextStrokePath(context);   
  }  
  • (void)setNeedsDisplay;
    标记为需要重绘,异步调用drawRect
    在UIView中,重写drawRect: (CGRect) aRect方法,可以自己定义想要画的图案.且此方法一般情况下只会画一次.也就是说这个drawRect方法一般情况下只会被掉用一次. 当某些情况下想要手动重画这个View,只需要掉用[self setNeedsDisplay]方法即可.

  • (void)setNeedsDisplayInRect:(CGRect)rect;
    标记为需要局部重绘

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容