ios开发 UI高级 图形上下文

来源:互联网 发布:济宁网络问政平台育才 编辑:程序博客网 时间:2024/05/22 15:23

DrawView.m

<span style="font-size:14px;">#import "DrawView.h"@implementation DrawView- (void)drawRect:(CGRect)rect {    // 获取上下文    CGContextRef ctx = UIGraphicsGetCurrentContext();        // 描述路径    // 第一根    UIBezierPath *path = [UIBezierPath bezierPath];        [path moveToPoint:CGPointMake(10, 150)];    [path addLineToPoint:CGPointMake(200, 150)];        // 把路径添加到上下文    // .CGPath 可以UIkit的路径转换成CoreGraphics路径    CGContextAddPath(ctx, path.CGPath);    // 保存一份上下文的状态    CGContextSaveGState(ctx);    // 设置上下文状态    CGContextSetLineWidth(ctx, 10);    [[UIColor cyanColor] set];    // 渲染上下文    CGContextStrokePath(ctx);        // 描述路径    path = [UIBezierPath bezierPath];    [path moveToPoint:CGPointMake(100, 10)];    [path addLineToPoint:CGPointMake(100, 200)];    // 把路径添加到上下文    CGContextAddPath(ctx, path.CGPath);    // 还原状态    CGContextRestoreGState(ctx);    // 渲染上下文    CGContextStrokePath(ctx);    }</span>


运行结果:

0 0
原创粉丝点击