
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结束。这

本文详细介绍了iOS开发中关于视图更新与重绘的关键方法:layoutSubviews、setNeedsLayout和layoutIfNeeded。layoutSubviews用于子视图的重新布局,不应直接调用,而应通过setNeedsLayout安排在下次绘图周期更新。layoutIfNeeded则用于立即更新视图布局,常在动画效果中使用。文章还通过实例验证了各方法的触发机制。
最低0.47元/天 解锁文章
294





