Quartz2D - 1

开源框架CorePlot

oc

oc使用方法:

- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    //1. 获得图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //2, 拼接图形(路径)
    //设置一个起点
    CGContextMoveToPoint(ctx, 100, 100);
    //添加一条线段到(100, 100)
    CGContextAddLineToPoint(ctx, 200, 200);
    //设置线宽
    CGContextSetLineWidth(ctx, 10);
    // 渲染显示到view上
    CGContextStrokePath(ctx);
    
    //clockwise: 1逆时针 0 顺时针
    CGContextAddArc(ctx, rect.size.width * 0.5, rect.size.height * 0.5, 100, 0, M_PI, 1);
    //设置颜色
    [[UIColor redColor] set];
    // 渲染填充显示到view上
    CGContextFillPath(ctx);
}

Swift

swift 使用context的方法:

  override func draw(_ rect: CGRect) {
        let cgContext: CGContext = UIGraphicsGetCurrentContext()! //获取当前上下文
        cgContext.move(to: CGPoint.init(x: 100, y: 100))
        cgContext.addLine(to: CGPoint.init(x: 200, y: 200))
        //设置线宽
        cgContext.setLineWidth(10) 
        //空心显示
        cgContext.strokePath() 
        //clockwise:true 逆时针, false顺时针
        cgContext.addArc(center: CGPoint.init(x: rect.size.width * 0.5, y: rect.size.height * 0.5), radius: 100, startAngle: 0, endAngle: CGFloat.pi, clockwise: true)
        //设置颜色
        UIColor.red.set() 
        //填充显示
        cgContext.fillPath()
    }
屏幕快照 2018-11-07 16.32.40.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,018评论 3 119
  • 秋 邑下风烟含翠微,江头每去鸟飞飞。天寒犹有黄花放,岁晚自知音信稀。江上笛声秋色白,人家池水草鱼肥。买鲜煮酒寻常日...
    杨青石阅读 292评论 0 0
  • 道德经第二十九章 将欲取天下而为之,吾见其不得已。天下神器,不可为也。为者败之,执者失之。夫物或行或随,或嘘或次,...
    蓝色滑行阅读 366评论 0 0