ios 图形与动画学习笔记 构造路径(CGPathCreateMutable)

/*

     路径属于我们正在绘制他们的上下文。路径没有边界(Boundary)或特定的形状,不想我们使用路径绘制出来的形状。

     但路径没有边界框(Bounding boxes此处,BoundaryBounding boxes完全不一样。

     边界显示你在画布上哪些不可以用来绘画,而路径的边界框是包含了所有路径的形状、点和其他已经绘制的对象的最小矩形。

     

     使用路径创建步骤:创建路径的方法返回一个路径的句柄,可以在绘制图形的使用就可以把句柄作为传递给core Graphics

                    当创建路径之后,可以向它添加不同的点、线条和形状,之后绘制图形。

     1CGPathCreateMutable 函数

        创建一个CGMutablePathRef 的可变路径,并返回其句柄。

     2CGPathMoveToPoint 过程

        在路径上移动当前画笔的位置到一个点,这个点由CGPoint 类型的参数指定。

     3CGPathAddLineToPoint 过程

        从当前的画笔位置向指定位置(同样由CGPoint类型的值指定)绘制线段

     4CGContextAddPath 过程

        添加一个由句柄指定的路径的图形上下文,准备用于绘图

     5CGContextDrawPath 过程

        在图形上下文中绘制给出的路径。

     6CGPathRelease 过程

        释放为路径句柄分配的内存。

     7CGPathAddRect 过程

        向路径添加一个矩形。矩形的边界由一个CGRect 结构体指定。

     */

     /*

     *创建一个新的可变路径(CGPathCreateMutable,把该路径加到你的图形上下文(CGContextAddPath

     *并把它绘制到图形上下文中(CGContextDrawPath

     */


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    /*
     *创建一个新的可变路径(CGPathCreateMutable),把该路径加到你的图形上下文(CGContextAddPath)
     *并把它绘制到图形上下文中(CGContextDrawPath)
     */
    
    /* Create the path */
    CGMutablePathRef path = CGPathCreateMutable();
    
    /* How big is our screen? We want the X to cover the whole screen */
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    
    /* Start from top-left */
    CGPathMoveToPoint(path, NULL,screenBounds.origin.x, screenBounds.origin.y);
    
    /* Draw a line from top-left to bottom-right of the screen */
    CGPathAddLineToPoint(path, NULL,screenBounds.size.width, screenBounds.size.height);
    
    /* Start another line from top-right */
    CGPathMoveToPoint(path, NULL,screenBounds.size.width, screenBounds.origin.y);
    
    /* Draw a line from top-right to bottom-left */
    CGPathAddLineToPoint(path, NULL,screenBounds.origin.x, screenBounds.size.height);
    
    /* Get the context that the path has to be drawn on */
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    
    /* Add the path to the context so we can draw it later */
    CGContextAddPath(currentContext, path);
    
    /* Set the blue color as the stroke color */
    [[UIColor blueColor] setStroke];
    
    /* Draw the path with stroke color */
    CGContextDrawPath(currentContext, kCGPathStroke);
    
    /* Finally release the path object */
    CGPathRelease(path);
    
    /*
     *传入CGPathMoveToPoint等过程的NULL参数代表一个既定的变换,在给定的路径绘制线条时可以使用此变换。
     */
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值