iOS redraw mechanism DrawRect

Source: Internet
Author: User

The drawing operations of iOS are done in the DrawRect method of the UIView class, so if we want to draw in a uiview, we need to write an extended UIView class and override the DrawRect method, where the drawing is done, and the program automatically calls this method to draw.
The following is a description of the drawing, for example, you want to draw a block, you need to write a class to extend the UIView and fill in the DrawRect method with the following code:
-(void) DrawRect: (cgrect) Rect {
Drawing code.
Gets the context of the processing
Cgcontextref context = Uigraphicsgetcurrentcontext ();
Set Line Style
Cgcontextsetlinecap (context, kcglinecapsquare);
Set Line thickness width
Cgcontextsetlinewidth (context, 1.0);

Set color
Cgcontextsetrgbstrokecolor (Context, 1.0, 0.0, 0.0, 1.0);
Start a start path
Cgcontextbeginpath (context);
The starting point is set to (0,0): Note that this is the relative coordinate in the corresponding area of the context,
Cgcontextmovetopoint (context, 0, 0);
Set the next coordinate point
Cgcontextaddlinetopoint (context, 100, 100);
Set the next coordinate point
Cgcontextaddlinetopoint (context, 0, 150);
Set the next coordinate point
Cgcontextaddlinetopoint (context, 50, 180);
Connect the coordinate points defined above
Cgcontextstrokepath (context);
}

Again, redrawing, redrawing is still done in the DrawRect method, but Apple does not recommend calling the DrawRect method directly, of course, if you call this method directly, of course, there is no effect. Apple requires us to invoke the Setneedsdisplay method in the UIView class, and the program automatically calls the DrawRect method for repainting. (Call Setneedsdisplay will automatically call DrawRect)
In UIView, rewrite drawrect: (cgrect) Arect method, you can define the pattern you want to draw. And this method is usually only drawn once. This means that the DrawRect method is normally only used once. When you want to manually redraw the view in some cases, you only need to drop the [self Setneedsdisplay] method.

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).

1. If the rect size is not set when UIView is initialized, the DrawRect is not automatically called.
2. This method is called after calling Sizethatfits, 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; 3,4 does not advocate
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. The same is called Setneeddisplay, and so on indirectly called 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

iOS redraw mechanism DrawRect

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.