文章目录
ViewController生命周期
加载流程
didReceiveMemoryWarning
View的layoutSubviews
Runloop相关
view的drawRect:方法
ViewController生命周期
加载流程

%title插图%num

1 1.init或者initWithCoder:(NSCoder *)aDecoder:(如果使用storyboard或者xib)
2 2.loadView:加载view
3 3.viewDidLoad:view加载完毕
4 4.viewWillAppear:控制器的view将要显示
5 5.viewWillLayoutSubviews:控制器的view将要布局子控件
6 6.viewDidLayoutSubviews:控制器的view布局子控件完成
7 这期间系统可能会多次调用viewWillLayoutSubviews、viewDidLayoutSubviews俩个方法
8
9 7.viewDidAppear:控制器的view完全显示
10 8.viewWillDisappear:控制器的view即将消失的时候
11 这期间系统也会调用viewWillLayoutSubviews 、viewDidLayoutSubviews 两个方法
12
13 9.viewDidDisappear:控制器的view完全消失的时候

didReceiveMemoryWarning

Discussion Your app never calls this method directly. Instead, this
method is called when the system determines that the amount of
available memory is low.

You can override this method to release any additional memory used by
your view controller. If you do, your implementation of this method
must call the super implementation at some point.

当app收到内存警告的时候会发消息给视图控制器。
app从来不会直接调用这个方法,而是当系统确定可用内存不足的时候采取调用。
如果你想覆写这个方法来释放一些控制器使用的额外内存,你应该在你的实现方法中调用父类的实现方法。

View的layoutSubviews
init初始化不会触发layoutSubviews。
addSubview会触发layoutSubviews。
改变一个UIView的frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化。
滚动一个UIScrollView引发UIView的重新布局会触发layoutSubviews。
旋转Screen会触发父UIView上的layoutSubviews事件。
直接调用setNeedsLayout 或者 layoutIfNeeded。

Runloop相关
在非主页面加载时

1 * thread #1, queue = ‘com.apple.main-thread’, stop reason = breakpoint 1.1
2   * frame #0: 0x0000000100529264 GSWatermarkView`-[GSWaterMarkView layoutSubviews](self=0x000000011d801410, _cmd=”layoutSubviews”) at GSWaterMarkView.m:110
3     frame #1: 0x00000001a77db5b0 UIKitCore`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2156
4     frame #2: 0x00000001a2fe7af0 libobjc.A.dylib`-[NSObject performSelector:withObject:] + 68
5     frame #3: 0x00000001a9d81c0c QuartzCore`-[CALayer layoutSublayers] + 292
6     frame #4: 0x00000001a9d81f14 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 484
7     frame #5: 0x00000001a9d953fc QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 140
8     frame #6: 0x00000001a9cda184 QuartzCore`CA::Context::commit_transaction(CA::Transaction*, double) + 296
9     frame #7: 0x00000001a9d05228 QuartzCore`CA::Transaction::commit() + 684
10     frame #8: 0x00000001a7362d6c UIKitCore`_afterCACommitHandler + 144
11     frame #9: 0x00000001a324bf5c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36
12     frame #10: 0x00000001a3246bfc CoreFoundation`__CFRunLoopDoObservers + 420
13     frame #11: 0x00000001a32471ac CoreFoundation`__CFRunLoopRun + 1292
14     frame #12: 0x00000001a3246978 CoreFoundation`CFRunLoopRunSpecific + 480
15     frame #13: 0x00000001ad376534 GraphicsServices`GSEventRunModal + 108
16     frame #14: 0x00000001a7338f0c UIKitCore`UIApplicationMain + 1940
17     frame #15: 0x000000010052814c GSWatermarkView`main(argc=1, argv=0x000000016f8df940) at main.m:14
18     frame #16: 0x00000001a30c6f04 libdyld.dylib`start + 4

在初始界面加载时

1 * thread #1, queue = ‘com.apple.main-thread’, stop reason = breakpoint 1.1
2   * frame #0: 0x0000000100b05264 GSWatermarkView`-[GSWaterMarkView layoutSubviews](self=0x000000010130f210, _cmd=”layoutSubviews”) at GSWaterMarkView.m:110
3     frame #1: 0x00000001a77db5b0 UIKitCore`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2156
4     frame #2: 0x00000001a2fe7af0 libobjc.A.dylib`-[NSObject performSelector:withObject:] + 68
5     frame #3: 0x00000001a9d81c0c QuartzCore`-[CALayer layoutSublayers] + 292
6     frame #4: 0x00000001a9d81f14 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 484
7     frame #5: 0x00000001a9d953fc QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 140
8     frame #6: 0x00000001a9cda184 QuartzCore`CA::Context::commit_transaction(CA::Transaction*, double) + 296
9     frame #7: 0x00000001a9d05228 QuartzCore`CA::Transaction::commit() + 684
10     frame #8: 0x00000001a7350d20 UIKitCore`__34-[UIApplication _firstCommitBlock]_block_invoke_2 + 84
11     frame #9: 0x00000001a324c95c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 28
12     frame #10: 0x00000001a324c0e0 CoreFoundation`__CFRunLoopDoBlocks + 268
13     frame #11: 0x00000001a32470e0 CoreFoundation`__CFRunLoopRun + 1088
14     frame #12: 0x00000001a3246978 CoreFoundation`CFRunLoopRunSpecific + 480
15     frame #13: 0x00000001ad376534 GraphicsServices`GSEventRunModal + 108
16     frame #14: 0x00000001a7338f0c UIKitCore`UIApplicationMain + 1940
17     frame #15: 0x0000000100b0414c GSWatermarkView`main(argc=1, argv=0x000000016f303940) at main.m:14
18     frame #16: 0x00000001a30c6f04 libdyld.dylib`start + 4

可以看出在app启动时,初始界面View的layoutSubviews由__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__触发,后续的界面由__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__触发

view的drawRect:方法
直接调用setNeedsDisplay,或者setNeedsDisplayInRect:触发drawRect:,但是有个前提条件是rect大小不能为0。
drawRect的调用时机是在viewWillAppear和viewDidAppear之间。且在View的layoutSubviews之后

1 2020-03-03 14:05:58.384185+0800 GSWatermarkView[1318:1216023]  ViewController [-[ViewController loadView]]
2 2020-03-03 14:05:58.384252+0800 GSWatermarkView[1318:1216023]  ViewController [-[ViewController viewDidLoad]]
3 2020-03-03 14:05:58.385593+0800 GSWatermarkView[1318:1216023]  GSWaterMarkView [-[GSWaterMarkView didMoveToSuperview]]
4 2020-03-03 14:05:58.385686+0800 GSWatermarkView[1318:1216023]  ViewController [-[ViewController viewWillAppear:]]
5 2020-03-03 14:05:58.387915+0800 GSWatermarkView[1318:1216023]  ViewController [-[ViewController viewWillLayoutSubviews]]
6 2020-03-03 14:05:58.387956+0800 GSWatermarkView[1318:1216023]  ViewController [-[ViewController viewDidLayoutSubviews]]
7 2020-03-03 14:05:58.387975+0800 GSWatermarkView[1318:1216023]  GSWaterMarkView [-[GSWaterMarkView layoutSubviews]]
8 2020-03-03 14:05:58.388046+0800 GSWatermarkView[1318:1216023]  GSWaterMarkView [-[GSWaterMarkView drawRect:]]
9 2020-03-03 14:05:58.427785+0800 GSWatermarkView[1318:1216023]  ViewController [-[ViewController viewDidAppear:]]

调用sizeToFit,会触发drawRect的调用。
通过设置contentMode属性值为UIViewContentModeRedraw。那么将在每次设置或更改frame的时候自动调用drawRect:。
app启动时,添加到初始界面的堆栈,还是CALayer触发