iOS CGContextRef画线(圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线)

本文介绍如何使用iOS的CoreGraphics框架中的CGContextRef进行图形绘制,包括圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形和贝塞尔曲线。通过示例代码展示各种图形的绘制方法。
摘要由CSDN通过智能技术生成

demo:[https://github.com/asd521411/iOS-CGContextRefDemo.git]

在这里插入图片描述
//
// CustomView.m
// CGContextRefDemo
//
// Created by hongbaodai on 2018/12/10.
// Copyright © 2018年 caomaoxiaozi. All rights reserved.
//

#import “CustomView.h”
#define PI 3.14159265358979323846

@implementation CustomView

  • (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    self.backgroundColor = [UIColor whiteColor];
    }
    return self;
    }

  • (void)drawRect:(CGRect)rect {
    [self drawCharacter];
    }

  • (void)drawCharacter {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor cyanColor].CGColor);//设置颜色
    //CGFontRef fontRef = UIGraphi
    //CGContextSetFont(context, <#CGFontRef _Nullable font#>)
    UIFont font = [UIFont systemFontOfSize:14];
    //[@“1” drawInRect:CGRectMake(10, 40, 100, 40) withFont:font];//废弃
    [@“画图1” drawInRect:CGRectMake(10, 40, 100, 40) withAttributes:@{NSAttachmentAttributeName:font,NSForegroundColorAttributeName:[UIColor cyanColor]}];
    //画图
    //画圆
    CGContextSetFillColorWithColor(context, [UIColor purpleColor].CGColor);//设置填充的画笔颜色
    //CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π (≈57.3°) 度=弧度×180°/π 360°=360×π/180 =2π 弧度
    // x,y为圆点坐标,radius半径,s

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值