Detailed examples of IOS drawing triangles


Detailed examples of IOS drawing triangles

Effect drawing first

The code of the triangle above

- (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]; 

Class for drawing triangles

#import "ljDrawRect.h" 
@interface ljDrawRect () 
  CGPoint _startPoint; 
  CGPoint _middlePoint; 
  CGPoint _endPoint; 
  UIColor *_color; 
@implementation ljDrawRect 
#pragma mark - 
#pragma mark - method 
- (instancetype)initStartPoint:(CGPoint)startPoint 
  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);// sign 
  CGContextMoveToPoint(context, _startPoint.x, _startPoint.y); 
  CGContextAddLineToPoint(context,_middlePoint.x, _middlePoint.y); 
  CGContextAddLineToPoint(context,_endPoint.x, _endPoint.y); 
  CGContextClosePath(context);// The path end flag is not written. It is closed by default 
  [_color setFill]; // Set fill color 
  [_color setStroke];// The border is also set to_ Color, otherwise it is the default black 
  CGContextDrawPath(context, kCGPathFillStroke);// Draw path path 

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

Recommended Today

Seven solutions for distributed transactions

1、 What is distributed transaction Distributed transaction means that transaction participants, transaction supporting servers, resource servers and transaction managers are located on different nodes of different distributed systems. A large operation is completed by more than n small operations. These small operations are distributed on different services. For these operations, either all of them are […]