IOS基础之绘图函数的使用

IOS基础之绘图函数的使用

IOS基础之绘图函数的使用_第1张图片

//
//  HMView.m
//  25-绘图步骤
//
//  Created by 鲁军 on 2021/2/14.
//

#import "HMView.h"

@implementation HMView

- (void)drawRect:(CGRect)rect {
     
    [self test11];
    //NSLog(@"%@",NSStringFromCGRect(rect));
    //{
     {0, 0}, {300, 300}}  打印的是当前view的bounds
    //[self setNeedsDisplayInRect:rect];
}
-(void)test11{
     
    //C hua yuan
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddArc(ctx, 150, 150, 100, 0, 2*M_PI, 1);
    CGContextStrokePath(ctx);
}

-(void)test10{
     
    //使用纯画圆函数 绘制圆形
    //ArcCenter 圆心
    //radius 半径
    //startAngle 起始位置
    //endAngle 结束位置
    //clockwise 是否是顺时针 1 逆时针。0。顺时针
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2*M_PI - M_PI_2 clockwise:YES];
    [path stroke];
}
-(void)test9{
     
    //C 画椭圆
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(ctx, CGRectMake(10, 10, 150, 100));
    CGContextStrokePath(ctx);
}
-(void)test8{
     
    //画椭圆
   // UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 100)];
   // [path stroke];
    //画圆
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];
    [path stroke];
}

-(void)test7{
     
    //带圆角的矩形的绘画
    //UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 100, 100) cornerRadius:10];
    //[path stroke];
    //画圆
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 100, 100) cornerRadius:50];
    [path stroke];
}
-(void)test6{
     
    //绘制矩形
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 100, 100)];
    [path stroke];
    
}

-(void)test5{
     
    // chun OC fang shi
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(100, 100)];
    [path stroke];
}
-(void)test4{
     
    //c + oc
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //拼接路径(c)
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 50, 50);
    CGPathAddLineToPoint(path, NULL, 100, 100);
    //拼接路径(oc)
    UIBezierPath *path1 = [UIBezierPath bezierPathWithCGPath:path];
    [path1 addLineToPoint:CGPointMake(150, 50)];
    //3.路径添加当前的上下文
    CGContextAddPath(ctx, path1.CGPath);
    //4渲染
    CGContextStrokePath(ctx);
}
-(void)test3{
     
    //oc + c
    // 1 获取 当前上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //拼接路径
    UIBezierPath *path = [[UIBezierPath alloc] init];
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(100, 250)];
    //3.路径添加当前的上下文
    CGContextAddPath(ctx, path.CGPath);
    CGContextStrokePath(ctx);
}
-(void)test2{
     
    //C的
    // 1 获取 当前上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2 拼接路径。
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 50, 50);
    CGPathAddLineToPoint(path, NULL, 100, 200);
    //3.路径添加当前的上下文
    CGContextAddPath(ctx, path);
    //4渲染
    CGContextStrokePath(ctx);
}
-(void)test1{
     
     // 1 获取 当前上下文
      CGContextRef ctx = UIGraphicsGetCurrentContext();
     //2 拼接路径。 同时把路径添加当前的上下文
     CGContextMoveToPoint(ctx, 50, 50); //起点
     CGContextAddLineToPoint(ctx, 100, 100);  //终点
     CGContextAddLineToPoint(ctx, 150, 50);  //终点
     CGContextMoveToPoint(ctx, 50, 200); //起点
     CGContextAddLineToPoint(ctx, 200, 200);  //终点
     //3。渲染
     CGContextStrokePath(ctx);
}
@end

你可能感兴趣的:(IOS)