Day.03.14UI绘图

ViewController.m

#import "ViewController.h"
#import "DrawerView.h"

#define KScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //有View >loadView >Xib >创建空白View >ViewDidLoad
    
    //DrawRect :方法 是绘制UIView的方法
    DrawerView *drawer = [[DrawerView alloc]initWithFrame:CGRectMake(0, 20, KScreenWidth, 400)];
    
    drawer.backgroundColor = [UIColor whiteColor];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    [self.view addSubview:drawer];
    
    
}
@end

DrawerView.m

#import "DrawerView.h"

@implementation DrawerView

- (void)drawRect:(CGRect)rect{

    NSLog(@"drawRact");
    
    [self drawLine];
}

- (void)drawLine{

//1.获取当前视图相关的上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
//2.路径
    //创建路径对象
    CGMutablePathRef path= CGPathCreateMutable();
    
    //起点
    CGPathMoveToPoint(path, NULL, 0, 5);
    
    //添加路径点
    CGPathAddLineToPoint(path, NULL, 300, 5);
    CGPathAddLineToPoint(path, NULL, 300, 200);
    
    //设置路径闭环
    CGPathCloseSubpath(path);
    
//3.上下文属性
    //填充颜色
    CGContextSetRGBFillColor(context, 212/255.0, 24/255.0, 148/255.0, 1);
    
    //线条宽度
    //如果是OC -->[context setLineWidth:5];
    CGContextSetLineWidth(context, 2);
    
    //线条颜色(默认黑色) (0,0,0)是黑色 (1,1,1)是白色
    CGContextSetRGBStrokeColor(context, 68/255.0, 178/255.0, 249/255.0, 1);
    
    //线条首尾样式
    /**
     *  kCGLineCapButt,
        kCGLineCapRound, 圆弧
        kCGLineCapSquare 方形
     */
    CGContextSetLineCap(context, kCGLineCapButt);
    
    //线条链接样式
    /**
     *  kCGLineJoinMiter,  默认
        kCGLineJoinRound,  圆弧
        kCGLineJoinBevel   斜线
     */
    CGContextSetLineJoin(context, kCGLineJoinBevel);
    
    //虚线定制
    /**
     *  context : 上下文对象
        phase : 虚线起点与路径起点的距离
        lengths : C数组 从数组中循环获取虚线长度和间隔
        count : 数组长度
     */
    CGFloat lengths[] = {15,15,15};
    
    CGContextSetLineDash(context, 0, lengths, 3);
    
//4.路径结合上下文
    CGContextAddPath(context, path);
    
//5.绘制路径
    /**
     *  kCGPathFill, 填充
        kCGPathEOFill,
        kCGPathStroke,   画线
        kCGPathFillStroke,  画线+填充
        kCGPathEOFillStroke
     */
    CGContextDrawPath(context, kCGPathStroke);
屏幕快照 2016-03-14 下午5.08.42.png
//6.释放路径
    CGPathRelease(path);

}

@end

屏幕快照 2016-03-14 下午5.05.12.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容