- Helps to understand how a view is rendered.
- Redrawing controls which is inherited from UIView
- Use of setNeedsDisplay: and setNeedsDisplayInRect: methods
UIView follows an interesting drawing model called an on-demand drawing model. When a view is rendered very first time the system draws the view and its contents and take a snapshot of the view's visual representation. Until the view's content is changed the system uses the same snapshot for rendering the view. So the views drawing code will never be called again until its contents are changed. If the contents are changed then the system takes the snapshot again and use it for rendering later.
When a content of the view is changed and needs to be redrawn you can invalidate the view by calling setNeedsDisplay: or setNeedsDisplayInRect: methods. When one of these method is called, it says the system to redraw the view. Before drawing the requested view the system will wait for the current run loop to finish. This delay help us to do further changes to the view's properties or you can even add or remove views from the hierarchy and finally all the changes will get effect at the end of the current run loop.
You should override the drawRect: method in your custom UIView only if you need to perform custom drawing. An empty implementation of drawRect: method will adversely affects the performance during animation. You should not call drawRect: method directly to redraw the view. Instead you should always call setNeedsDisplay: or setNeedsDisplayInRect: methods.
setNeedsDisplay: method causes the entire rectangle of the view to be redrawn whereas setNeedsDisplayInRect: method causes the specified rectangle of the view to be redrawn.
One interesting point to note here is changing the view's geometry does not cause the system to redraw the view's content. The view's contentMode property determines how the changes to the view's geometry are interpreted. Other than UIViewContentModeRedraw, most contentMode values causes the system to just stretch or reposition the existing snapshot of the view within the view's boundaries.
Summary:
- UIView follows an on-demand drawing model.
- System takes a snapshot of the view and reuses it for the next time.
- System will redraw the view only when its contents are changed.
- To redraw the view you should not call drawRect: method directly instead you should call setNeedsDisplay: or setNeedsDisplayInRect: method.
- For custom drawing you should override drawRect: method in the UIView.
very useful information about view rendering in ios...great..keep blogging...
ReplyDeleteVery Nice info regarding view invalidate.
ReplyDeletewhether view will attempt to refresh at particular interval?