IOS Swift Basic Drawing tutorial

Source: Internet
Author: User

OS Swift Basic Drawing tutorial

In fact, this is an example that has been done before, to facilitate their own reference to the code! I hope you have a little reference.

First, create a swift class, inherit the UIView class, and then rewrite

Func DrawRect (Rect:cgrect)

Second, get the context of the brush

var context:cgcontextref = Uigraphicsgetcurrentcontext ();//Get Brush Context cgcontextsetallowsantialiasing (context, true)// Anti-aliasing settings


So we can write the code for the drawing.

1 draw points

Draw points

Cgcontextfillellipseinrect (Context, CGRectMake (75, 75, 50, 50))


2 Draw a line

Draw Straight Line Cgcontextsetlinewidth (context, 5)//Set Brush width cgcontextmovetopoint (context, 10, 20);        Cgcontextaddlinetopoint (context, 100, 100); Cgcontextstrokepath (context)

3 Draw a Circle

Draw round Cgcontextaddellipseinrect (context, CGRectMake (50,50,100,100)); Draw Circle Cgcontextstrokepath (context)//close path//Draw circle by arc//radians = angle multiplied by π and divided by 180//angle = The radian is divided by π and multiplied by 180CGContextSetStrokeColorWithColor (context, Uicolor.redcolor (). Cgcolor)//Set Brush color Cgcontextaddarc (context, 0, CGFloat (270*m_pi/180), 0)//ARC Cgcontextstrokepath (context)/ /close Path



4 Draw a String

Draw string var str:nsstring = "I am Wu Shi"; Str.drawatpoint (Cgpointmake (s), withattributes:nil);


5 Drawing Pictures

Draw Picture Cgcontextsetshadow (Context, Cgsizemake (3, 3), ten) var img:uiimage = UIImage (named: "8")!; Img.drawatpoint (Cgpointmake (50, 250)); Cgcontextsetshadow (context, Cgsizemake (0, 0), 0)

6 Using Path Paint

Use path paint let P1:cgmutablepathref = cgpathcreatemutable (); Cgpathmovetopoint (P1, Nil, $) cgpathaddlinetopoint (P1, nil, +) Cgcontextaddpath (context, p1) Cgcontextstrokepath (context)//close path

Here is mainly the introduction of basic usage, other complex graphics, you can refer to the API documentation, to call the relevant methods, draw the graphics you want

Complete code is included:

  DrawingView.swift//  study////  Created by Tonway on  15/2/2.//  copyright  (c)  2015 year  tonway. all rights reserved.//import  UIKitclass DrawingView: UIView {             // Only override drawRect: if you perform custom  drawing.    // an empty implementation adversely affects  Performance during animation.    override func drawrect (rect:  CGRect)  {        // Drawing code                 var context:cgcontextref  =  uigraphicsgetcurrentcontext ();//Get Brush context                &nBsp;         cgcontextsetallowsantialiasing (Context, true)  //anti-aliasing settings                 / /Draw Point         //cgcontextsetlinewidth (context, 50);         cgcontextfillellipseinrect (Context, cgrectmake (75, 75, 50,  50))                  Cgcontextsetlinewidth (context, 5)  //set brush width                  //Draw Line          Cgcontextmovetopoint (context, 10, 20);         Cgcontextaddlinetopoint (context, 100, 100);         Cgcontextstrokepath (context)                 //Circle          cgcontextaddellipseinrect (Context, cgrectmake (50,50,100,100));  //Circle                  cgcontextstrokepath (context)  // Close Path                          //Circle         //radians by drawing arcs = The angle is multiplied by pi and then divided by 180        //angle = Radian divided by π and multiplied by 180         cgcontextsetstrokecolorwithcolor (Context, uicolor.redcolor (). Cgcolor)  //Set Brush color                  cgcontextaddarc (Context, 100, 100, 50, 0, cgfloat (270*M_PI/180),  0)  //Painting Arc          &Nbsp;              cgcontextstrokepath ( Context)//close path                                  //Drawing Strings          var str:NSString =  "I am Wu Shi wei";                 str.drawatpoint (CGPointMake (100,  ,  withattributes: nil);                 //Picture                  cgcontextsetshadow (Context, cgsizemake (3, 3), ten)                  var img:uiimage = uiimage ( named:  "8")!;  &nBsp;              img.drawatpoint ( Cgpointmake (50, 250));                         // cgcontextdrawimage (context,  CGRectMake (100, 250, 100, 100), IMG. Cgimage)                  Cgcontextsetshadow (Context, cgsizemake (0, 0),  0)                  //Using path Paint          let p1:cgmutablepathref = cgpathcreatemutable ();                 cgpathmovetopoint (p1, nil, 50, 250)                 cgpathadDlinetopoint (p1, nil, 50, 350)                  cgcontextaddpath (CONTEXT, P1)          cgcontextstrokepath (context)//close path                                                        }        }

This article belongs to Wu Shi Wei's blog original article, reproduced when please indicate the source and the corresponding link: http://www.wutongwei.com/front/infor_showone.tweb?id=85

IOS Swift Basic Drawing tutorial

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.