年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1821|回复: 0

Quartz2D的基本使用(第二讲:基本图形绘制)

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-12-1 11:34:46 | 显示全部楼层 |阅读模式
    一、基本绘图方法
    1、移动画笔位置方法形式如  CGContextMovexxxx, 如:CGContextMoveToPoint(CGContextRef c, CGFloat x, CGFloat y)为移动到坐标(x, y)

    2、描述图形方法如 CGContextAddxxxx,如:
    CGContextAddLineToPoint(CGContextRef c, CGFloat x, CGFloat y),描述一个线段从当前画笔点到点(x,y)
    GContextAddRect(CGContextRef c, CGRect rect),根据传入的rect描述一个矩形,rect中指定了位置与尺寸
    CGContextAddEllipseInRect(CGContextRef context, CGRect rect),根据传入的rect描述一个在rect中能容纳的最大椭圆,如果为rect中width与height相等则为圆
    CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise),该函数为画一段圆弧,传入的参数分别为:上下文,圆心x坐标,圆心y坐标, 半径, 开始弧度值,结束弧度值,顺时钟(1为顺时钟,0为逆时钟)

    3、图形设置方法如 CGContextSetxxxx,如:
    CGContextSetRGBFillColor(CGContextRef context, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha),填充图形时使用的颜色RGB与透明度值

    4、图形渲染 CGContextxxxxPath(),如:
    CGContextFillPath(CGContextRef c), 渲染为实心图形
    CGContextStrokePath(CGContextRef c), 渲染为空心图形

    二、示例代码

    1. //
    2. //  SAView.m
    3. //  Quartz2D
    4. //
    5. //  Created by 余西安 on 14/12/1.
    6. //  Copyright (c) 2014年 Sian. All rights reserved.
    7. //
    8. #import "SAView.h"
    9. @implementation SAView
    10. -(void)drawRect:(CGRect)rect
    11. {
    12.     // 1、获取上下文(开启当前绘图板)
    13.     CGContextRef ctx = UIGraphicsGetCurrentContext();
    14.    
    15.     // 2、绘制图形
    16.     // 2.1 画矩形
    17.     // 绘制矩形,输入上下文及矩形的大小尺寸Rect
    18.     // CGContextAddRect(CGContextRef c, CGRect rect)
    19.     CGContextAddRect(ctx, (CGRect){50, 50, 100, 100});
    20.     // 渲染空心图形到当前View
    21.     CGContextStrokePath(ctx);
    22.    
    23.     // 2.2 画圆(椭圆)
    24.     // 绘制椭圆(圆是椭圆的一种),同样指定上下文与圆所占据的Rect即可
    25.     // CGContextAddEllipseInRect(CGContextRef context, CGRect rect)
    26.     CGContextAddEllipseInRect(ctx, (CGRect){100, 200, 100, 100});
    27.     // 渲染实心图形到当前View
    28.     CGContextFillPath(ctx);
    29.    
    30.     // 2.3 画圆弧(圆)
    31.     // 将“画笔”移动到画弧的起点,如果没有这句会从上次绘图的终点“拖线”到当前绘图起点
    32.     CGContextMoveToPoint(ctx, 200, 400);
    33.     // 绘制圆弧,指定圆弧的圆心、半径、弧度起点与终点,绘制方向(顺时钟或逆时钟)
    34.     // CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)
    35.     CGContextAddArc(ctx, 150, 400, 50, 0, M_PI * 2, 0);
    36.     // 设置填充颜色RGB值
    37.     // CGContextSetRGBFillColor(CGContextRef context, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha)
    38.     CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
    39.     // 渲染实心图形到当前View
    40.     CGContextFillPath(ctx);
    41. }
    42. @end
    复制代码
    三、运行效果

    iOS 模拟器屏幕快照“2014年12月1日 上午11.33.59”.png



    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-4-29 03:47 , Processed in 0.054052 second(s), 21 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表