"Go" uiview need to know some things: Setneedsdisplay, setneedslayout

Source: Internet
Author: User

Original: http://blog.sina.com.cn/s/blog_923fdd9b0101b2b4.html

1. The parent class of Nswindow in Mac OS is Nsresponder, and the parent class UIWindow in iOS is UIView. Programs typically have only one window but many views.

2, the role of UIView: Painting and animation, the view is responsible for its own rectangular area mapping, layout and sub-view management, event processing, can receive touch events, the carrier of event information, and so on.

3. Uiviewcontroller is responsible for creating a view of its management and removing them from memory in low memory. It also responds to the standard system behavior.

4. Layoutsubviews can reload this method in its own custom view to adjust the size and position of the sub-view.

5, UIView Setneedsdisplay and Setneedslayout method. The first two methods are executed asynchronously. And Setneedsdisplay will call the automatic call DrawRect method, so you can get uigraphicsgetcurrentcontext, you can draw. Setneedslayout, by default, calls Layoutsubviews to handle some of the data in the child view.

In the case of the pope, Setneedsdisplay convenient drawing, and layoutsubviews convenient data

1,Setneedsdisplay and Setneedslayout methods of UIView

The first two methods are executed asynchronously. And Setneedsdisplay will call the automatic call DrawRect method, so you can get uigraphicsgetcurrentcontext, you can draw. And Setneedslayout will call layoutsubviews by default, you can work with some of the data in the child view. In full , Setneedsdisplay convenient drawing, and layoutsubviews convenient data. < Span style= "padding:0px; margin:0px; " > layoutsubviews is called in the following cases: < Span style= "padding:0px; margin:0px; Color: #333333; " > 1, init initialization does not trigger layoutsubviews.
2, Addsubview will trigger layoutsubviews.
3, Setting the frame of the view will trigger Layoutsubviews, of course, if the frame's value has changed before and after it has been set.
4, Scrolling a uiscrollview will trigger layoutsubviews.
5, Rotating screen triggers the Layoutsubviews event on the parent UIView.
6, Changing the size of a uiview also triggers the Layoutsubviews event on the parent UIView. 7, call Setlayoutsubviews directly.
The DrawRect is called in the following cases:

  1, if the rect size is not set at UIView initialization, it will directly cause drawrect not to be automatically called. drawrect call is in controller->loadview, controller- After the >viewDidLoad  two method is dropped. So don't worry. In the controller, these view drawrect begin to draw. This allows you to set some values in the controller to the view (if these view  Some variable values are required for draw).

< Span style= "padding:0px; margin:0px; Color: #333333; " >2, this method calls Sizetfit is called after, so you can call SizeToFit to calculate the size first. The system then automatically calls the DrawRect: method.
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.
above 1, 2 recommended; and 3, 4 not advocated
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 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

"Go" uiview need to know some things: Setneedsdisplay, setneedslayout

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.