IOS quartz2d Draw lines, rectangles, arcs, circles, text, pictures

Source: Internet
Author: User

Simple drawing of lines, rectangles, arcs, circles, text, pictures using the layer context in quartz2d

Drag the controller into the storyboard, add multiple UIView controls, and modify each UIView class to a custom one.

Such as:

Draw lines:

hjlineview.m//Draw line Triangle rectangle round////Created by Hjiang on 15/1/2.//Copyright (c) 2015 Hjiang. All rights reserved.//#import "HJLineView.h"/** * Draw line */@implementation hjlineview/** * When this method is called * 1. Manually invoke [self Setneedsdis  Play] method is called after the DrawRect method * 2. Custom View the first time the rendering is called the DrawRect method DrawRect method is called after the controller's Viewdidload method, also called after the SizeToFit method * @param rect < #rect description#> */-(void) DrawRect: (cgrect) rect{//Get online cgcontextref CTX = Uigraphicsgetcurrentcon        Text ();        Set the corresponding Properties//settings Color Cgcontextsetrgbstrokecolor (CTX, 1.0, 0.0, 0.0, 0.9);        Set line width (default one is 1) cgcontextsetlinewidth (CTX, 5);        Sets the line's Kinsoku style Cgcontextsetlinecap (CTX, Kcglinecapround);        Set the connection point style of the line Cgcontextsetlinejoin (CTX, Kcglinejoinround);    Set the starting point Cgcontextmovetopoint (CTX, 10, 20);        Set connection point Cgcontextaddlinetopoint (CTX, 40, 80);        2nd Line//Set connection point Cgcontextaddlinetopoint (CTX, 90, 10); Draw Line Cgcontextstrokepath (CTX);} @end

Draw Rectangle:

hjrectangleview.m//Draw line Triangle rectangle round////Created by Hjiang on 15/1/2.//Copyright (c) 2015 Hjiang. All rights reserved.//#import "HJRectangleView.h"/** *//Draw Rectangle */@implementation hjrectangleview-(void) DrawRect: (CGRect ) rect{[self drawWay4];}        /** * Draw Hollow Square */-(void) drawway1{cgcontextref ctx = Uigraphicsgetcurrentcontext ();    Cgcontextsetlinewidth (CTX, 2);    Set Hollow Line Color cgcontextref context, CGFloat red: Digital/255.0,cgfloat Green: Digital/255.0, CGFloat Blue: Digital/255.0, cgfloat Alpha: transparency        Cgcontextsetrgbstrokecolor (CTX, 0.0, 1.0, 0.0, 0.7);        Cgcontextmovetopoint (CTX, 10, 10);        Cgcontextaddlinetopoint (CTX, 60, 10);        Cgcontextaddlinetopoint (CTX, 60, 60);        Cgcontextaddlinetopoint (CTX, 10, 60);    Mode 1 Connection starting point//Cgcontextaddlinetopoint (CTX, 10, 10);        Mode 2 closed Path Cgcontextclosepath (CTX); Draw Hollow Rectangle Cgcontextstrokepath (CTX);}        /** * Draw Hollow Square */-(void) drawway2{cgcontextref ctx = Uigraphicsgetcurrentcontext (); CgcontextsetlinewiDTH (CTX, 2);        Set the hollow Line color Cgcontextsetrgbstrokecolor (CTX, 0.0, 1.0, 0.0, 0.7);        Set the starting point and size Cgcontextaddrect (CTX, CGRectMake (10, 10, 70, 70)); Draw Hollow Rectangle Cgcontextstrokepath (CTX);}        /** * Draw a solid square */-(void) drawway3{cgcontextref ctx = Uigraphicsgetcurrentcontext ();        Cgcontextsetlinewidth (CTX, 2);        Sets the solid fill color cgcontextsetrgbfillcolor (ctx, 0.0, 1.0, 0.0, 0.7);        Cgcontextmovetopoint (CTX, 10, 10);        Cgcontextaddlinetopoint (CTX, 60, 10);        Cgcontextaddlinetopoint (CTX, 60, 60);        Cgcontextaddlinetopoint (CTX, 10, 60);    Mode 1 Connection starting point//Cgcontextaddlinetopoint (CTX, 10, 10);        Mode 2 closed Path Cgcontextclosepath (CTX); Draw a solid rectangle cgcontextfillpath (CTX);}        /** * Draw a solid square */-(void) drawway4{cgcontextref ctx = Uigraphicsgetcurrentcontext ();        Cgcontextsetlinewidth (CTX, 2);        Sets the solid fill color cgcontextsetrgbfillcolor (ctx, 0.0, 1.0, 0.0, 0.7); Set the starting point and size Cgcontextaddrect (CTX, CGRectMake (10, 10, 70, 70)); Draw a solid rectangle cgcontextfillpath (CTX);} @end

To draw a circle:

hjcircleview.m//Draw line Triangle rectangle round////Created by Hjiang on 15/1/2.//Copyright (c) 2015 Hjiang.    All rights reserved.//#import "HJCircleView.h"/** * Draw Circle */@implementation hjcircleview-(void) DrawRect: (CGRect) rect{//    [Self drawarc];//[self drawsector]; [Self drawcircle];}        /** * arc * x, y Center * radius radius * StartAngle The starting position of the arc * Endangel arc End position * Clockwise 0 Shun pin 1 counterclockwise */-(void) drawarc{        Cgcontextref CTX = Uigraphicsgetcurrentcontext (); /* ADD an arc of a circle to the context's path, possibly preceded by a straight line segment. ' (x, y) ' is the center of the arc; ' Radius ' is its radius;     ' StartAngle ' is the angle to the first endpoint of the arc; ' Endangle ' is the angle to the second endpoint of the arc;     and ' clockwise ' is 1 if the arc was to be drawn clockwise, 0 otherwise. ' StartAngle ' and ' endangle ' is measured in radians.        */////Draw arc Cgcontextaddarc (CTX,-m_pi_4, M_PI, 0); Draw Render Cgcontextstrokepath (CTX);} /** *Draw Fan *x,y Center *radius radius *startangle The starting position of the arc *endangel the end position of the arc * Clockwise 0 Shun pin 1 counterclockwise */-(void) drawsector{Cgcontext        Ref CTX = Uigraphicsgetcurrentcontext ();        Connect the arc starting point, based on a point, and then close the path Cgcontextmovetopoint (CTX, 60, 40); /* ADD an arc of a circle to the context's path, possibly preceded by a straight line segment. ' (x, y) ' is the center of the arc; ' Radius ' is its radius;     ' StartAngle ' is the angle to the first endpoint of the arc; ' Endangle ' is the angle to the second endpoint of the arc;     and ' clockwise ' is 1 if the arc was to be drawn clockwise, 0 otherwise. ' StartAngle ' and ' endangle ' is measured in radians.        */////Arc Cgcontextaddarc (CTX,-m_pi_4, m_pi_4*5, 0);        Close path Cgcontextclosepath (CTX); Draw Render//Cgcontextstrokepath (CTX);    Hollow Cgcontextfillpath (CTX);        }/** * Draw round */-(void) drawcircle{cgcontextref ctx = Uigraphicsgetcurrentcontext (); Set color//CGCONTEXTSETRGBSTROKECOlor (CTX, 1.0, 0, 0, 0.7);        Cgcontextsetrgbfillcolor (CTX, 0, 1.0, 0, 0.7);        Sets the size of the hollow Circle Line//Cgcontextsetlinewidth (CTX, 3);        The circle drawn by the size of the RECT can be elliptical cgcontextaddellipseinrect (CTX, CGRectMake (20, 10, 90, 80));    Hollow Circle//Cgcontextstrokepath (CTX);    Solid round Cgcontextfillpath (CTX); } @end

Draw text:

  hjwordview.m//  draw line triangle rectangle round////  Created by Hjiang on 15/1/2.//  Copyright (c) 2015 Hjiang. All rights reserved.//#import "HJWordView.h"/** *  draw text into view * */@implementation hjwordview-(void) DrawRect: ( CGRect) rect{        cgfloat w = rect.size.width;    CGFloat h = rect.size.height;        Draw Text    NSString *wordtext = @ "text rendering to view on 111";    Nsdictionary *attrs = @{nsfontattributename:[uifont Systemfontofsize:13],nsforegroundcolorattributename:[uicolor Redcolor]};        Tested 2 ways can//    [Wordtext drawatpoint:cgpointmake (5, 5) Withattributes:nil];    [Wordtext Drawinrect:cgrectmake (5, 5, W, h) withattributes:attrs];    } @end

To draw a picture:

hjimageview.m//Draw line Triangle rectangle round////Created by Hjiang on 15/1/2.//Copyright (c) 2015 Hjiang. All rights reserved.//#import "HJImageView.h"/** * Draw a picture * Note: When you draw multiple content in this method, it is possible that the content that is drawn later will overwrite the previous content, primarily the person who first looks behind and the area has no overlap */@implem    Entation hjimageview-(void) DrawRect: (cgrect) rect{cgfloat w = rect.size.width;        CGFloat h = rect.size.height;    Draw picture UIImage *image = [UIImage imagenamed:@ "Papa"];    CGRect inrect = CGRectMake (10, 10, 100, 100);    Cgpoint point = Cgpointmake (10, 40);    mode 1//[Image Drawatpoint:point];    Mode 2 [Image Drawinrect:inrect];    Mode 3 tile effect//[image drawaspatterninrect:inrect];    Mode 4 Mixing effect//[Image Drawinrect:inrect Blendmode:kcgblendmodesoftlight alpha:1.0];            mode 5//[Image Drawatpoint:point Blendmode:kcgblendmodesoftlight alpha:0.7];    Draw text NSString *wordtext = @ "text rendering"; Nsdictionary *attrs = @{nsfontattributename:[uifont Systemfontofsize:13],nsforegroundcolorattributename:[uicolor        Redcolor]}; Tested 2 ways toTo//[Wordtext Drawatpoint:cgpointmake (5, 5) Withattributes:nil]; [Wordtext Drawinrect:cgrectmake (N, W, h) withattributes:attrs];} @end

IOS quartz2d Draw lines, rectangles, arcs, circles, text, pictures

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.