Draw a straight line

Source: Internet
Author: User

Method 1 (recommended): Use calayer

CALayer *middleBorder = [CALayer layer];middleBorder.frame = CGRectMake(x, y, width, height);middleBorder.backgroundColor = UIColor.CGColor;[myView.layer addSublayer:middleBorder];

Method 2: Use uiimageview (not easy to change)

1-(void) drawlinewithpoint :( cgpoint) startpoint topoint :( cgpoint) topoint 2 {3 cgsize screensize = [[uiscreen mainscreen] applicationframe]. size; 4 5 uiimageview * imageview = [[uiimageview alloc] initwithframe: cgrectmake (0, 0, screensize. width, screensize. height)]; 6 // uiimageview * imageview = [[uiimageview alloc] init]; 7 // imageview. frame = self. contentview. frame; 8 [self. contentview addsubview: imageview]; 9 10 uigraphicsbeginimagecontext (imageview. frame. size); 11 [imageview. image drawinrect: cgrectmake (0, 0, imageview. frame. size. width, imageview. frame. size. height)]; 12 13 // obtain the processing context 14 cgcontextref context = uigraphicsgetcurrentcontext (); 15 16 // specify the line style 17 cgcontextsetlinecap (context, kcglinecapsquare ); 18 19 // straight line width 20 cgcontextsetlinewidth (context, 1.0); 21 22 // set color 23 // RED: 166/255. 0 Green: 177/255. 0 Blue: 186/255. 024 cgcontextsetrgbstrokecolor (context, 246.0/255.0, 247.0/255.0, 247.0/255.0, 1.0); 25 26 // start to draw 27 cgcontextbeginpath (context ); 28 29 // the paint brush moves to the point (31,170) 30 cgcontextmovetopoint (context, startpoint. x, startpoint. y); 31 32 // 33 cgcontextaddlinetopoint (context, topoint. x, topoint. y); 34 35 // 36 cgcontextstrokepath (context); 37 38 imageview. image = uigraphicsgetimagefromcurrentimagecontext (); 39 uigraphicsendimagecontext (); 40 41 // nslog (@ "% F, % F", imageview. frame. size. width, imageview. frame. size. height); 42}

 

Draw a straight 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.