iOS rewrite DrawRect method implements a view with arrows

Source: Internet
Author: User

Create a subclass of UIView, overriding the DrawRect method to implement an irregular-shaped view, which provides an implementation code with an arrow view:

ArrowView.h

#import <UIKit/UIKit.h>@interface  arrowview:uiview@end

arrowview.m

#import "ArrowView.h"@implementationArrowview/*//Only override drawrect:if your perform custom drawing.//an empty implementation adversely affects performance Duri ng animation.-(void) DrawRect: (cgrect) Rect {//Drawing code}*/-(instancetype) init{ Self=[Super Init]; if(self) {Self.backgroundcolor=[Uicolor Whitecolor]; }    returnSelf ; }-(Instancetype) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {Self.backgroundcolor=[Uicolor Whitecolor]; }    returnSelf ; }- (void) DrawRect: (CGRect) rect{[Super Drawrect:rect]; NSLog (@"being DrawRect ..."); //gets the graph of the current drawing that the view pushes into the stack, which is equivalent to the drawing you want to drawCgcontextref CTX =Uigraphicsgetcurrentcontext (); //[[Uicolor Whitecolor]Set]; //Create a new empty graphics pathCgcontextbeginpath (CTX); NSLog (@"Start drawing ..."); //starting position coordinatesCGFloat origin_x =rect.origin.x; CGFloat origin_y=Ten;//FRAME.ORIGIN.Y + 10; //position coordinates of the first lineCGFloat line_1_x = rect.size.width- -; CGFloat line_1_y=origin_y; //position coordinates of the second lineCGFloat line_2_x = line_1_x +5; CGFloat line_2_y=RECT.ORIGIN.Y; //position coordinates of the third lineCGFloat line_3_x = line_2_x +5; CGFloat line_3_y=line_1_y; //position coordinates of the fourth lineCGFloat line_4_x =Rect.size.width; CGFloat line_4_y=line_1_y; //position coordinates of the fifth lineCGFloat line_5_x =Rect.size.width; CGFloat line_5_y=Rect.size.height; //position coordinates of the sixth lineCGFloat line_6_x =origin_x; CGFloat line_6_y=Rect.size.height;        Cgcontextmovetopoint (CTX, origin_x, origin_y);    Cgcontextaddlinetopoint (CTX, line_1_x, line_1_y);    Cgcontextaddlinetopoint (CTX, line_2_x, line_2_y);    Cgcontextaddlinetopoint (CTX, line_3_x, line_3_y);    Cgcontextaddlinetopoint (CTX, line_4_x, line_4_y);    Cgcontextaddlinetopoint (CTX, line_5_x, line_5_y);        Cgcontextaddlinetopoint (CTX, line_6_x, line_6_y);        Cgcontextclosepath (CTX); //Set Fill ColorUicolor *customcolor = [Uicolor colorwithwhite:0Alpha0.8];    Cgcontextsetfillcolorwithcolor (CTX, Customcolor.cgcolor); Cgcontextfillpath (CTX);}@end

Then call in Viewcontroller to see the results

Viewcontroller.m

#import "ViewController.h"#import "ArrowView.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.NSLog (@"begin ..."); Arrowview*view = [[Arrowview alloc] Initwithframe:cgrectmake ( -, -, $, the)]; //[view Setbackgroundcolor:[uicolor Orangecolor];[Self.view Addsubview:view]; NSLog (@"End ...");}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

Results:

Console Print Results:

The thread ID of the console print is the same, stating that the DrawRect method is called on the main thread.

iOS rewrite DrawRect method implements a view with arrows

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.