绘图之iOS笔记摘录

目录

  1. UIBezierPath
  2. Core Graphics

1. UIBezierPath(属UIKit框架,对Core Graphics进行了封装)

常用于
  1、动画的路径
  2、CAShapeLayer的路径

系统自带路径

    // 矩形
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 40, 10, 10)];
    //  圆角矩形
    UIBezierPath *path2 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 100, 30) cornerRadius:10];
    // 特殊的圆角矩形(可以选择性裁剪角)
    UIBezierPath *path3 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 40, 10, 10) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(100, 100)];
    // 椭圆
    UIBezierPath *path4 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 20, 200, 30)];
    // 圆弧
    UIBezierPath *path5 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 20) radius:10 startAngle:M_PI endAngle:M_PI*2 clockwise:true];
    UIBezierPath *path6 = [UIBezierPath bezierPath];
    UIBezierPath *path7 = [UIBezierPath bezierPathWithCGPath:path.CGPath];
只能在当前上下文中绘图:
    UIGraphicsBeginImageContextWithOptions 或 drawRect:(占内存CPU)中可直接使用
    若持有一个context:参数,使用之前,必须将该上下文参数转化为当前上下文。调用UIGraphicsPushContext转化为当前上下文,最后调用UIGraphicsPopContext恢复上下文。
    // 自定义路径
    UIBezierPath *path=[UIBezierPath new];
    // 启始点
    [path moveToPoint:CGPointMake(0, 0)];
    // 添加直线
    [path addLineToPoint:CGPointMake(100, 0)];
    // 添加贝圆弧(圆心,半径,起始角,结束角,是否顺时针)
    [path addArcWithCenter:CGPointMake(100, 10) radius:10 startAngle:M_PI*3/2 endAngle:M_PI/2 clockwise:true];
    // 添加贝塞尔曲线
    [path addCurveToPoint:CGPointMake(100, 20) controlPoint1:CGPointMake(20, 30) controlPoint2:CGPointMake(20, 50)];
    [path addQuadCurveToPoint:CGPointMake(100, 20) controlPoint:CGPointMake(50, 50)];
    // 闭合路径(连接起始点)
    [path closePath];
    // 设置线宽
    [path setLineWidth:1.0];
    // 设置线的两端点style
    [path setLineCapStyle:kCGLineCapRound];
    /*
     kCGLineCapButt,     // 无端点
     kCGLineCapRound,    // 圆形端点
     kCGLineCapSquare    // 和无端点差不多,但是要长一点
     */
    // 设置线的连接点style
    [path setLineJoinStyle:kCGLineJoinRound];
    /*
     kCGLineJoinMiter,    // 尖角
     kCGLineJoinRound,    // 圆角
     kCGLineJoinBevel    // 切角
     */
    // 最大斜接长度(拐角的内尖角和外尖角的长度),lineJoinStyle模式下
    [path setMiterLimit:10];
    // 将UIBezierPath类转换成CGPath
    path.CGPath;
    // 当前path的位置(readOnly)
    CGPoint point=path.currentPoint;
    // 当前path的bounds(readOnly)
    CGRect bounds=path.bounds;
    // 是否空路径(readOnly)
    BOOL isEmpty=path.isEmpty;

    // 是否包含某点
    [path containsPoint:CGPointMake(19, 29)];

    // 清除所有路径
    [path removeAllPoints];
    // 追加路径
    [path appendPath:path2];
    // 设置填充色
    [[UIColor redColor]setFill];
    // 设置边框色
    [[UIColor grayColor]setStroke];
    // 设置边框色、填充色
    [[UIColor blueColor]set];
    
    // 填充
    [path fill];
    // 边框色
    [path stroke];
    // 圆角裁剪
    [path addClip];

文本、图片

    NSString *contentStr=@"";
    [contentStr drawInRect:CGRectMake(0, 0, 100, 20) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]}];
    // 文本的左上角
    [contentStr drawAtPoint:CGPointMake(100, 20) withAttributes:@{}];
    [contentStr drawWithRect:CGRectMake(0, 0, 100, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{} context:nil];

    
    UIImage *img=[UIImage imageNamed:@""];
    // 按比例
    [img drawInRect:CGRectMake(0, 0, 100, 200)];
    // 平铺
    [img drawAsPatternInRect:CGRectMake(0, 0, 100, 20)];
    // 图片的左上角
    [img drawAtPoint:CGPointMake(100, 20)];
    [img drawInRect:CGRectMake(0, 0, 100, 20) blendMode:kCGBlendModeHue alpha:1.0];
    
2. Core Graphics

更底层强大,所有绘图的基础,又称QuartZ或QuartZ 2D

三个核心概念
  上下文:主要用于描述图形写入哪里;
  路径:是在图层上绘制的内容;
  状态:用于保存配置变换的值、填充和轮廓, alpha 值等。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容