iOS开发-layoutSubviews、setNeedsLayout、layoutIfNeeded的爱恨情仇

iOS 中 UIKit 为 UIView 提供了这些方法来进行视图的更新与重绘:

//更新方法
- (void)setNeedsLayout;
- (void)layoutIfNeeded;

- (void)layoutSubviews;

//重绘方法
- (void)drawRect:(CGRect)rect;

- (void)setNeedsDisplay;
- (void)setNeedsDisplayInRect:(CGRect)rect;

View Drawing Cycle

要理解视图的更新与重绘,那么就必然要知道 iOS 的绘图机制 The View Drawing Cycle。

那么这个 The View Drawing Cycle 到底是什么呢,官方是这样解释的:

The system waits until the end of the current run loop before initiating any drawing operations. This delay gives you a chance to invalidate multiple views, add or remove views from your hierarchy, hide views, resize views, and reposition views all at once. All of the changes you make are then reflected at the same time.

翻译:在开始任何绘图操作之前,系统将一直等到当前RunLoop结束。这种延迟使您有机会使多个视图失效、从层次结构中添加或删除视图、隐藏视图、调整视图大小以及同时重新定位视图。然后,您所做的所有更改都会同时反映出来。

显然修改 UIView 的 frame、bounds 等属性后并不会马上重新绘制,而是用 RunLoop 把多次绘制聚集在一个 Cycle 里一起渲染,这显然是

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值