IOS Learning Notes---One of the most basic artboards (pure Code implementation)

Source: Internet
Author: User

Create an iOS project in the OC-developed language, creating a new class inheritance with UIView

Rewrite the method and implement

//declare two instance variables inside the. h file{cgpoint _startpoint;//record the position when the click is slidingnsmutablearray* _marray;//record the position when sliding    }//inside the. m file//overriding initialization methods-(ID) initWithFrame: (cgrect) frame{if(self==[Super Initwithframe:frame]) {Self.backgroundcolor=[uicolor Lightgraycolor];//set Background to gray_marray=[nsmutablearrayNew]; }       returnSelf ;}//Touch Start Method-(void) Touchesbegan: (nsset*) touches withevent: (uievent*)Event{Uitouch* Touch = [touches anyobject];//gets a value that is arbitrary when clicked_startpoint = [Touch locationinview:self];//get the location when you clickNsmutablearray *pointarray = [NsmutablearrayNew];//       //Convert to ObjectNsvalue *value =[Nsvalue Valuewithcgpoint:_startpoint]; //Saves the value of the clicked position to the newly created array[Pointarray Addobject:value]; [_marray Addobject:pointarray];}//How to slide the mouse-(void) touchesmoved: (nsset*) touches withevent: (uievent*)Event{       //similar to the Start method, just gets the last one of the _marray array and adds the obtained position to it.Nsmutablearray *pointarray =[_marray Lastobject]; Uitouch* touch=[touches anyobject]; Cgpoint Currentpoint=[Touch locationinview:self]; Nsvalue* value=[Nsvalue Valuewithcgpoint:currentpoint];           [Pointarray Addobject:value]; //This method echoes the painting method .[Self setneedsdisplay];//Show side Draw}//method At the end of the mouse-(void) touchesended: (nsset*) touches withevent: (uievent*)Event{}//Painting Method-(void) DrawRect: (cgrect) rect{cgcontextref CGRF= Uigraphicsgetcurrentcontext ();//Set Strokes//Thickness of strokeCgcontextsetlinewidth (CGRF,2.0f); //Color of strokesCgcontextsetstrokecolorwithcolor (Cgrf,[uicolor Yellowcolor].       Cgcolor);  for(nsmutablearray* Pointarrinch_marray) {         for(intI=0; i<pointarr.count-1; i++) {Nsvalue* value=Pointarr[i]; Cgpoint Apoint=[value cgpointvalue]; //move a brush to a specified pointCgcontextmovetopoint (CGRF, Apoint.x, APOINT.Y); Nsvalue* nextvalue=pointarr[i+1]; Cgpoint Nextapoint=[NextValue Cgpointvalue]; //wire The brush from the current point to the given point (for parameters of the function)Cgcontextaddlinetopoint (CGRF, Nextapoint.x, NEXTAPOINT.Y);    }} cgcontextstrokepath (CGRF); }

Pay attention to implementing it in VIEWCONTROLLER.M.

Import #import "Draw.h"

-(void) viewdidload {    [super Viewdidload];     // additional setup after loading the view, typically from a nib.    self.view=[[Draw Alloc]initwithframe:[uiscreen mainscreen].bounds];}

I created draw, just need to change the custom file name to

So a simple artboard is done.

It's complicated. You can add controls on the artboard, change the brush color

IOS Learning Notes---One of the most basic artboards (pure Code implementation)

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.