IOS 绘制三角形的实例详解
程序员文章站
2023-12-18 15:13:22
ios 绘制三角形的实例详解
先上效果图
上面三角形的代码
- (void)ljtestview
{
cgpoint piont1;...
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
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!