IOS绘制各种图形(三角形,圆形,矩形,椭圆等)

三角形代码如下,放到一个自定义View里即可运行:

<pre name="code" class="objc">- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [super drawRect:rect];
    
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    [[UIColor whiteColor] set];
     CGContextFillRect(contextRef, rect);
    CGContextSetLineCap(contextRef, kCGLineCapSquare);
    CGContextSetRGBStrokeColor(contextRef,0.314, 0.486, 0.859, 1.0);
    CGContextBeginPath(contextRef);
    CGContextMoveToPoint(contextRef, 160, 80);
    CGContextAddLineToPoint(contextRef, 0, 100);
    CGContextAddLineToPoint(contextRef,320 , 100);
    CGContextAddLineToPoint(contextRef,160, 80);

    CGContextStrokePath(contextRef);
    
}

 
 

画圆(里面不填充颜色)

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [super drawRect:rect];
    
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    [[UIColor whiteColor] set];
     CGContextFillRect(contextRef, rect);
    
    [[UIColor redColor] set];
    CGContextAddEllipseInRect(contextRef, CGRectMake(200.0f, 200.0f, 50.0f, 50.0f));

    CGContextStrokePath(contextRef);
    
}
如果里面填充颜色,只要把CGContextAddEllipseInRect改成CGContextFillEllipseInRect就可以了

画矩形:

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [super drawRect:rect];
    
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    [[UIColor whiteColor] set];
     CGContextFillRect(contextRef, rect);
    
    [[UIColor redColor] set];
   // CGContextAddEllipseInRect(contextRef, CGRectMake(200.0f, 200.0f, 50.0f, 50.0f));
    CGContextFillRect(contextRef, CGRectMake(200.0f, 200.0f, 50.0f, 50.0f));
    CGContextStrokePath(contextRef);
    
}


画曲线
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [super drawRect:rect];
    
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    [[UIColor whiteColor] set];
     CGContextFillRect(contextRef, rect);
    
    [[UIColor redColor] set];
  
    CGContextSetLineWidth(contextRef, 2.0);
    CGContextSetStrokeColorWithColor(contextRef, [UIColor blueColor].CGColor);
    CGContextMoveToPoint(contextRef, 10, 10);
    CGContextAddCurveToPoint(contextRef, 200, 50, 100, 400, 300, 400);
    
    CGContextStrokePath(contextRef);
    
}
其中CGContextAddCurveToPoint的参数含义是它有1个起点,1个终点,其余两个是控制点,决定曲线的走向,可以如下图所示


用手指画线:(viewController里带有一个imageview,然后有一个prePoint的属性,下面的代码是写在viewController里的):

- (UIImage *)drawLineWithColor:(UIColor *)color width:(CGFloat)width startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint
{
    UIImage *image = nil;
    
    UIGraphicsBeginImageContext(self.imageview.frame.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetLineWidth(context, width);
    CGContextSetStrokeColorWithColor(context, [color CGColor]);
    
    CGContextMoveToPoint(context, startPoint.x, startPoint.y);
    CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
    
    CGContextStrokePath(context);
    
    image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}

#pragma mark - deal touch
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesBegan");
    //下面两句知道手指在屏幕上的点的信息
    UITouch* touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.imageview];
    
    if (touch) {
        self.prePoint = point;
    }
    NSLog(@"x=%f,y=%f",point.x,point.y);
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesMoved");
    UITouch* touch = [touches anyObject];
    if (touch) {
        CGPoint point = [touch locationInView:self.imageview];
        self.imageview.image = [self drawLineWithColor:[UIColor redColor] width:3.0f startPoint:self.prePoint endPoint:point];
        
    }
    
}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesEnded");
}



猜你喜欢

转载自blog.csdn.net/baidu_nod/article/details/32943855