您好,登錄后才能下訂單哦!
IOS 繪制三角形的實例詳解
先上效果圖
上面三角形的代碼
- (void)ljTestView { CGPoint piont1; piont1.x = 170; piont1.y = 100; CGPoint piont2; piont2.x = 50; piont2.y = 200; CGPoint piont3; piont3.x = 220; piont3.y = 200; ljDrawRect *_ljView = [[ljDrawRect alloc]initStartPoint:piont1 middlePoint:piont2 endPoint:piont3 color:[UIColor redColor]]; _ljView.frame = CGRectMake(0, 64, kDEVICEWIDTH, kDEVICEHEIGHT - 64); _ljView.backgroundColor = [UIColor yellowColor]; [self.view addSubview:_ljView]; }
畫三角形的類
#import "ljDrawRect.h" @interface ljDrawRect () { CGPoint _startPoint; CGPoint _middlePoint; CGPoint _endPoint; UIColor *_color; } @end @implementation ljDrawRect #pragma mark - #pragma mark - method - (instancetype)initStartPoint:(CGPoint)startPoint middlePoint:(CGPoint)middlePoint endPoint:(CGPoint)endPoint color:(UIColor*)color { self = [super init]; if (self) { _startPoint = startPoint; _middlePoint = middlePoint; _endPoint = endPoint; _color = color; self.backgroundColor = [UIColor clearColor]; } return self; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextBeginPath(context);//標記 CGContextMoveToPoint(context, _startPoint.x, _startPoint.y); CGContextAddLineToPoint(context,_middlePoint.x, _middlePoint.y); CGContextAddLineToPoint(context,_endPoint.x, _endPoint.y); CGContextClosePath(context);//路徑結束標志,不寫默認封閉 [_color setFill]; //設置填充色 [_color setStroke];//邊框也設置為_color,否則為默認的黑色 CGContextDrawPath(context, kCGPathFillStroke);//繪制路徑path } @end
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。