当前位置 : 主页 > 手机开发 > ios >

IOS 绘制三角形的实例详解

来源:互联网 收集:自由互联 发布时间:2021-05-10
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;

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 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

网友评论