UIView's Setneedsdisplay and Setneedslayout methods. The first two methods are executed asynchronously . Setneedsdisplay will call the automatic call DrawRect method, so that you can get uigraphicsgetcurrentcontext, you can draw. Setneedslayout, by default, calls Layoutsubviews to handle some of the data in the child view.
All two methods are executed asynchronously,layoutsubviews convenient data calculation, DrawRect convenient view redraw .
Let's take a look at some of the ways the iOS layout mechanism relates:
| 1 2 3 |
-(Cgsize) Sizethatfits: (cgsize) Size -(void) SizeToFit |
| 1 2 3) 4 5 |
-(void) layoutsubviews-(void) layoutifneeded-(void) setneedslayout |
| 1 2 3 |
-(void) Setneedsdisplay -(void) drawrect |
First, The Layoutsubviews is called in the following cases:
1. Init initialization does not trigger layoutsubviews.
2, Addsubview will trigger layoutsubviews.
3, set the frame of the view will trigger Layoutsubviews, of course, if the frame value has changed before and after the setting.
4, rolling a uiscrollview will trigger layoutsubviews.
5. Rotating screen will trigger the Layoutsubviews event on the parent UIView.
6, changing the size of a uiview will also trigger the parent UIView on the Layoutsubviews event.
7, call Setlayoutsubviews directly.
8, call Setneedslayout directly.
In Apple's official document, it is emphasized that you should override this method only if the autoresizing behaviors of the subviews does not offer the behavior y ou want.
Layoutsubviews, we need to call when we are adjusting the child view position inside a class.
In turn, this means: If you want to set the Subviews location externally, do not rewrite it.
Refresh child Object Layout
-layoutsubviews method: This method, the default does not do anything, need to rewrite the subclass
-setneedslayout method: Marked for the need to re-layout, asynchronous call layoutifneeded refresh the layout, not immediately refreshed, but layoutsubviews must be called
-layoutifneeded method: If there are tags that need to be refreshed, call layoutsubviews immediately to lay out (without a tag, layoutsubviews will not be called)
If you want to refresh immediately, call [view Setneedslayout], set the tag to need layout, and immediately call [view layoutifneeded] to implement the layout
before the view is first displayed, the tag always "needs to be refreshed" and can be called directly [view layoutifneeded]
Second, The DrawRect is called in the following cases:
1. If the rect size is not set at UIView initialization, the drawrect will not be automatically called. The DrawRect is dropped after the Controller->loadview, Controller->viewdidload two method. So don't worry. In the controller, The drawrect of the view began to draw. This allows you to set some values in the controller to the view (if some variable values are needed for these view draw).
2, this method is called after calling SizeToFit, so you can call SizeToFit to calculate the size first. The system then automatically calls the DrawRect: method.
SizeToFit will automatically call the Sizethatfits method;
SizeToFit should not be overridden in subclasses and should be rewritten sizethatfits
Sizethatfits The parameter passed in is the current size of receiver and returns a suitable size
SizeToFit can be manually called directly
SizeToFit and Sizethatfits methods are not recursive, to subviews is not responsible, only responsible for their own
3, by setting the Contentmode property value to Uiviewcontentmoderedraw. The drawrect will be called automatically each time the frame is set or changed:.
4. Call Setneedsdisplay directly, or setneedsdisplayinrect: Trigger DrawRect:, but there is a precondition that rect cannot be 0.
-setneedsdisplay method: Mark as required redraw, call DrawRect asynchronously
-setneedsdisplayinrect: (CGRect) Invalidrect method: Marked to require partial redraw
Above 1, 2 recommended; 3,4 does not advocate
DrawRect methods use note points:
1, if using UIView drawing, only in the DrawRect: method to obtain the corresponding Contextref and drawing. If obtained in other methods, it gets to a invalidate ref and cannot be used for paint. DrawRect: The method cannot manually display the call, and the system must be automatically tuned by calling Setneedsdisplay or Setneedsdisplayinrect.
2, if using Calayer drawing, can only be drawn in Drawincontext: (similar to fish drawrect), or in the corresponding method of delegate. Also call Setneeddisplay and so on indirectly call the above method
3, to real-time paint, can not use Gesturerecognizer, can only use Touchbegan and other methods to drop the Setneedsdisplay real-time refresh screen
Third, Layoutsubviews to subviews re-layout
Layoutsubviews method invocation precedes DrawRect
Setneedslayout a tag that needs to be re-laid on the receiver label and is automatically called on the next cycle of the system Runloop layoutsubviews
Layoutifneeded the name of the method, Uikit will determine if the receiver needs layout. According to Apple's official documentation, the Layoutifneeded method should be
Layoutifneeded traversal is not the Superview chain, it should be the subviews chain
DrawRect is a redraw of receiver that gets the context
Setneeddisplay a marker that needs to be redrawn on the receiver label, automatically redrawn on the next draw cycle, and iphone device refresh rate is 60hz, which is 1/60 seconds after redrawing
UIView layoutsubviews and DrawRect in iOS