Simple learning of iPhone re-painting mechanism

Source: Internet
Author: User

IPhone redrawMechanism is the content to be introduced in this article, mainly to explain how to useIPhoneProceedDrawing and redrawingOperation.IPhone redrawThe Mechanism gives people the biggest feeling, strange!

IPhonePlottingOperations are completed in the drawRect method of the UIView class, so if we wantPlotting, You need to write a class that extends the UIView and override the drawRect method.PlottingThe program automatically calls this method.Plotting.

The following describes the plot first. For example, if you want to draw a square, you need to write a class to extend the UIView and fill in the following code in the drawRect method:

 
 
  1. Code block       
  2.  
  3. - (void)drawRect:(CGRect)rect   
  4. {   
  5. // Drawing code CGContextRef context=UIGraphicsGetCurrentContext();   
  6. CGContextSetLineWidth(context, width);   
  7. CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);   
  8. CGContextStrokeRect(context, CGRectMake(110.0, 110.0, 100.0, 100.0));  
  9.  CGContextStrokePath(context);   


The effect is as follows:

In addition, the re-painting operation is still completed in the drawRect method, but apple does not recommend that you directly call the drawRect method. Of course, if you call this method directly with rigidity, it is of course ineffective. Apple asked us to call the setNeedsDisplay method in the UIView class, then the program will automatically call the drawRect Method for re-painting. As shown in:

In, click the "circle" button to draw a circle on the screen, click the "painting" button to draw a square on the screen, drag the slider to adjust the width of the line.

Summary simple learningIPhone redrawI hope this article will help you with the introduction of the mechanism!

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.