Redrawing mechanism for iPhone development applications

Source: Internet
Author: User

IPhone DevelopmentThe application repainting mechanism is the content to be introduced in this article. Let's explain it today.IPhone DevelopmentIn the redraw mechanism, the iPhone redraw mechanism gives people the biggest feeling, strange. Next we will explain how to use the iPhonePlottingAnd redraw operations.

The drawing operation in iPhone development is completed in the drawRect method of the UIView class. Therefore, if we want to plot in a UIView, We need to write a class that extends the UIView and override the drawRect method, the program will automatically call this method for 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. // Drawing code CGContextRef context=UIGraphicsGetCurrentContext();   
  5. CGContextSetLineWidth(context, width);   
  6. CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);   
  7. CGContextStrokeRect(context, CGRectMake(110.0, 110.0, 100.0, 100.0));   
  8. 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:IPhone DevelopmentThe redrawing mechanism of the application has been introduced. I hope this article will help you!

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.