When the layoutSubviews and drawRect methods of UIView are called, layoutsubviews are not called
First, both methods are executed asynchronously.LayoutSubviews facilitate data computing and drawRect facilitate view re-painting.
LayoutSubviews will be called in the following cases: 1. layoutSubviews will not be triggered during init initialization.
2. addSubview triggers layoutSubviews.
3. Setting the view Frame triggers layoutSubviews. Of course, the premise is that the frame value has changed before and after setting.
4. Rolling a UIScrollView triggers layoutSubviews.
5. Rotating Screen triggers the layoutSubviews event on the parent UIView.
6. Changing the size of a UIView triggers the layoutSubviews event on the parent UIView. 7. Call setLayoutSubviews directly. DrawRect is called in the following cases: 1. If the rect size is not set during UIView initialization, The drawRect is not automatically called. DrawRect is called after the Controller-> loadView, Controller-> viewDidLoad method. so don't worry about the drawRect of these views in the controller. in this way, you can set some values for the View in the Controller (if some variable values are required for the View draw ).
2. This method is called after sizeToFit is called. Therefore, you can call sizeToFit to calculate the size. Then the system automatically calls drawRect: method.
3. Set the value of contentMode to UIViewContentModeRedraw. The drawRect: is automatically called every time the frame is set or changed :.
4. Call setNeedsDisplay directly or setNeedsDisplayInRect to trigger drawRect:, but there is a precondition that the rect cannot be 0.
The preceding 1, 2, and 3 do not advocate the drawRect method. Note: 1. If UIView is used for plotting, the corresponding contextRef can only be obtained and drawn in the drawRect: method. If it is obtained in other methods, an invalidate ref will be obtained and cannot be used for drawing. DrawRect: The call method cannot be displayed manually. You must call setNeedsDisplay or setNeedsDisplayInRect to enable the system to automatically call this method.
2. If calayer is used for plotting, it can only be drawn in drawInContext: (similar to drawRect) or in the corresponding method of delegate. The preceding method is also called indirectly, such as setNeedDisplay.
3. To draw images in real time, you cannot use gestureRecognizer. You can only use methods such as touchbegan to use setNeedsDisplay to refresh the screen in real time.