DrawRect and drawRect

Source: Internet
Author: User

DrawRect and drawRect

Before starting, we need to create a DrawRectView

 

The initial code is

//// DrawRectView. h // CGContextSetShouldAntialias // Created by YouXianMing on. // Copyright 2017 TechCode. all rights reserved. // # import <UIKit/UIKit. h> @ interface DrawRectView: UIView @ end
//// DrawRectView. m // CGContextSetShouldAntialias // Created by YouXianMing on. // Copyright 2017 TechCode. all rights reserved. // # import "DrawRectView. h "@ implementation DrawRectView-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {self. backgroundColor = [UIColor clearColor]; self. layer. borderWidth = 0.5f; self. layer. borderColor = [UIColor redColor]. CGColor;} return self;} @ end

 

Used in ViewController (X in size and centered)

 

The display effect is as follows (the border is displayed with a red border)

 

The following code modifies DrawRectView. m:

//// DrawRectView. m // CGContextSetShouldAntialias // Created by YouXianMing on. // Copyright 2017 TechCode. all rights reserved. // # import "DrawRectView. h "@ implementation DrawRectView-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {self. backgroundColor = [UIColor clearColor]; self. layer. borderWidth = 0.5f; self. layer. borderColor = [UIColor redColor]. CGColor;} return self;}-(void) drawRect :( CGRect) rect {// Set stroke color [[UIColor blackColor] setStroke]; // Draw 7 lines. for (int I = 0; I <7; I ++) {UIBezierPath * path = [UIBezierPath bezierPath]; path. lineWidth = 0.5f; [path moveToPoint: CGPointMake (10, 10 + I * 10.3)]; [path addLineToPoint: CGPointMake (10 + 80, 10 + I * 10.3)]; [path stroke] ;}}@ end

 

In fact, the following drawing code is added to draw 7 lines, with the width of each line being 0.5

 

The effect is as follows:

 

After enlarging the image, you will find that the width of the line is inconsistent. Some colors are deep and some are light. This is the effect after anti-aliasing is enabled.

 

Modify code to disable anti-aliasing

//// DrawRectView. m // CGContextSetShouldAntialias // Created by YouXianMing on. // Copyright 2017 TechCode. all rights reserved. // # import "DrawRectView. h "@ implementation DrawRectView-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {self. backgroundColor = [UIColor clearColor]; self. layer. borderWidth = 0.5f; self. layer. borderColor = [UIColor redColor]. CGColor;} return self;}-(void) drawRect :( CGRect) rect {// Get context. CGContextRef context = UIGraphicsGetCurrentContext (); // Sets anti-aliasing on. CGContextSetShouldAntialias (context, NO); // Set stroke color [[UIColor blackColor] setStroke]; // Draw 7 lines. for (int I = 0; I <7; I ++) {UIBezierPath * path = [UIBezierPath bezierPath]; path. lineWidth = 0.5f; [path moveToPoint: CGPointMake (10, 10 + I * 10.3)]; [path addLineToPoint: CGPointMake (10 + 80, 10 + I * 10.3)]; [path stroke] ;}}@ end

 

Display Effect

 

After the image is enlarged, the line width is consistent.

 

 

Conclusion

After anti-aliasing is enabled, the system will blur the drawn lines to make it difficult to see the fangs. What is the fangs? You can run the following code to see the difference between the two.

//// DrawRectView. m // CGContextSetShouldAntialias // Created by YouXianMing on. // Copyright 2017 TechCode. all rights reserved. // # import "DrawRectView. h "@ implementation DrawRectView-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {self. backgroundColor = [UIColor clearColor]; self. layer. borderWidth = 0.5f; self. layer. borderColor = [UIColor redColor]. CGColor;} return self;}-(void) drawRect :( CGRect) rect {// Get context. CGContextRef context = UIGraphicsGetCurrentContext (); // Sets anti-aliasing off. CGContextSetShouldAntialias (context, NO); // Set stroke color [[UIColor blackColor] setStroke]; // Draw 7 lines. for (int I = 0; I <7; I ++) {UIBezierPath * path = [UIBezierPath bezierPath]; path. lineWidth = 0.5f; [path moveToPoint: CGPointMake (0, 0 + I * 10.3)]; [path addLineToPoint: CGPointMake (10 + 80, 10 + I * 10.3)]; [path stroke] ;}}@ end

 

After the anti-aliasing function is disabled, the image will not be blurred, and the image will also appear.

 

 

The line is blurred after the anti-aliasing function is enabled, which is called anti-aliasing.

 

 

 

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.