iOS Quartz2D 常用方法总结

来源:互联网 发布:java 转换base64编码 编辑:程序博客网 时间:2024/05/20 06:54
 - (void)drawRect:(CGRect)rect {

    //    这里是Layer Graphics Context

    CGContextRef  ctx = UIGraphicsGetCurrentContext();

//    绘制图形

//    设置起点

    CGContextMoveToPoint(ctx, 1010);

//    设置终点 ctx 是上下文

    CGContextAddLineToPoint(ctx, 10100);

//    绘制图形(渲染到layer上)view

    CGContextStrokePath(ctx);

    

}

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

//    获得图形的上下文

    CGContextRef ctx = UIGraphicsGetCurrentContext();

//    绘制图形的起点

    CGContextMoveToPoint(ctx, 500);

//    设置第二个点

    CGContextAddLineToPoint(ctx, 0100);

//    设置第三个点

    CGContextAddLineToPoint(ctx, 100100);

//    进行封口操作

    CGContextClosePath(ctx);

//    渲染

    CGContextStrokePath(ctx);

    CGContextAddRect(ctx, CGRectMake(5010050100));

    [[UIColor redColor]set];

//    CGContextStrokePath(ctx);

    CGContextFillPath(ctx);

    

}



// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

//当自定义的view即将显示的时候 就会调用这个方法

- (void)drawRect:(CGRect)rect {

    //    这里是Layer Graphics Context

    CGContextRef  ctx = UIGraphicsGetCurrentContext();

//    绘制图形

//    设置起点

    CGContextMoveToPoint(ctx, 1010);

//    设置终点 ctx 是上下文

    CGContextAddLineToPoint(ctx, 10100);

    CGContextAddLineToPoint(ctx, 100100);

//    设置绘图状态

//    设置线条颜色

    CGContextSetRGBStrokeColor(ctx, 1.0001.0);

//    设置线条宽度

    CGContextSetLineWidth(ctx, 10.2);

//    设置线条圆头

    CGContextSetLineCap(ctx, kCGLineCapRound);

//    设置线条转角的圆形

    CGContextSetLineJoin(ctx, kCGLineJoinRound);

//    绘制图形(渲染到layer上)view

    CGContextStrokePath(ctx);

    

}





新建一个起点

void CGContextMoveToPoint(CGContextRef c, CGFloat x, CGFloat y)


添加新的线段到某个点

void CGContextAddLineToPoint(CGContextRef c, CGFloat x, CGFloat y)


添加一个矩形

void CGContextAddRect(CGContextRef c, CGRect rect)


添加一个椭圆

void CGContextAddEllipseInRect(CGContextRef context, CGRect rect)


添加一个圆弧

void CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y,

  CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)




/**

 *  绘制平行线

 *

 *  @param rect <#rect description#>

 */

- (void)drawRect:(CGRect)rect {

//    注意这是一个C语言的函数 没有*

    CGContextRef  ctx = UIGraphicsGetCurrentContext();

    CGPoint addLines[] = {CGPointMake(10.0200),CGPointMake(50.0100),CGPointMake(90.0200),CGPointMake(130.0100),CGPointMake(170.0200),CGPointMake(210.0100)};

//    绘制

    CGContextStrokeLineSegments(ctx, addLines, sizeof(addLines)/sizeof(addLines[0]));

}

- (void)drawRect:(CGRect)rect {

    

//    绘制渐变色

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGColorSpaceRef color = CGColorSpaceCreateDeviceRGB();//创建色彩空间

//    开始设置颜色

    UIColor * start = [UIColor blueColor];

    CGFloat * startColorComp = (CGFloat *)CGColorGetComponents([start CGColor]);

//    设置结束颜色

    UIColor *end = [UIColor yellowColor];

    CGFloat * endColorComp = (CGFloat *)CGColorGetComponents([end CGColor]);

//  创建颜色分量数组

    CGFloat colorComponents[8] = {

        startColorComp[0],startColorComp[1],startColorComp[2],startColorComp[3],endColorComp[0],endColorComp[1],endColorComp[2],endColorComp[3],

    };

//    指定渐变开始位置和渐变结束位置

    CGFloat colorIndices[2] = {0.0f ,1.0f,};

//    创建渐变

    CGGradientRef gradient = CGGradientCreateWithColorComponents(color, (const CGFloat *)&colorComponents, (const CGFloat *)&colorIndices, 2);

    CGPoint startPoint ,endPoint;

    startPoint = CGPointMake(120260);

    endPoint = CGPointMake(200.0200);

    CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, 0);

}
图片的填充

- (void)drawRect:(CGRect)rect {

    UIImage * image = [UIImage imageNamed:@"abc"];

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGRect re = CGRectMake(60606060);

    CGContextClipToRect(ctx, CGRectMake(00320480));

    CGContextDrawTiledImage(ctx, re, image.CGImage);

}

  

0 0
原创粉丝点击