IOS Cgcontextref draw a straight line, just draw a line

Source: Internet
Author: User

Today's weekend rest, want to make up for the lessons, helpless, got a morning, all are half pull block of the demo, there is a deep sense of frustration.

After waking up at noon, I saw a episode of "Running the brothers" and then one afternoon elapsed.

Careful thinking, it should be my remedial method is not correct: to fill the things too much, you have to start from a large place, if you start from a small place, it has to spend the time of the old nose to get.

Then, start with the lines that have been bothering you for a long time.

The first two days to do the project, the chief let me use the line to achieve a function, he said the line I will be afraid, because this aspect is not very familiar, these days tinker a few son, but each time is doing this, and found another not to the east, to other battlefield, and so on, the time has passed, Draw line This function still did not solve, deep frustration feeling again hit a, also no mood to get, then wash went to sleep, the next day and again.

Now found their own strategy is wrong, so to timely correction ah!

The following is a good summary of the basic skills of the line, if other Bo friends see, can not tell the maze!

  

Write in front:

1. First you must know: if you want to draw a line, you must write in the DrawRect method, that is, to override this method.

2. Must know the meaning of the context, I think is to get the current view of the canvas.

These few places are clear, it is not difficult.

1. First, you should create a project

2. In project, create a new view that inherits from UIView, which we define as Lineview;

3. Overriding the Init method

3.1 In this, we want to define the current view of the backgroundcolor, because the default is black, we set it to white.

  

-(Instancetype) init{

if (self = [super init]) {

Self.backgroundcolor = [Uicolor Whitecolor];

}

return self;

}

4. Overriding the DrawRect method

4.1 To draw a line, we have to set the canvas first.

Cgcontextref context = Uigraphicsgetcurrentcontext ();

4.2 Set the start and end point of this line: from the upper-left corner of the screen-the lower-right corner of the screen

Cgcontextmovetopoint (context, 0, 0);//Establish a starting point first

Cgcontextaddlinetopoint (context, [[UIScreen Mainscreen]bounds].size.width, [[UIScreen mainscreen]bounds]. Size.Height);//Set the end point. If there are more than two points, you can call this method repeatedly, there will be more than one polyline

The previous context represents the coordinates of the current point, based on the current canvas, followed by two parameters.

4.3 Set the width of the current line

Cgcontextsetlinewidth (context, 5.0);//The larger the number, the thicker the line

4.4 Set the color of the current line

CGFloat components[] = {221.0/255,221.0/255,221.0/255,1.0f};

Cgcontextsetstrokecolor (context, components);

Components are used

[Uicolor colorwithred:<# (cgfloat) #> green:<# (cgfloat) #> blue:<# (cgfloat) #> alpha:<# (CGFloat) # ;]

This method is separated, filled in the array, the range is from 0.0-1.0, can not be crossed.

4.5 Connect the two points

Cgcontextstrokepath (context);

In this way, the simple line is finished.

  

But this is not enough, because we have not affixed it to the viewcontroller inside.

In the Viewdidload method in viewcontroller.m, we add it in:

-(void) Viewdidload {

[Super Viewdidload];

Self.view.backgroundColor = [Uicolor Whitecolor];

Lineview * Lineview = [[Lineview alloc]init];

Lineview.frame = Self.view.bounds;

[Self.view Addsubview:lineview];

}

Run the program, this light gray line on the rough ~ ~ ~ Function Although simple, but, can complete the writing, also counted a small sense of accomplishment. Of course, this is just a small start, more complicated task, still waiting for us in the back!

A new group has just been created: iOS Advanced Learning Exchange Group

QQ Group No.: 461093715

I am the only one here, but I believe there will be more friends to join us, we progress together.

Beijing today's weather is very cool Ah, the morning did not open the air conditioning, opened the fan, frozen my small short legs straight shiver, now open the window, small wind straight to come.

IOS Cgcontextref draw a straight line, just draw a line

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.