图形上下文栈

本文通过一个具体的iOS绘图实例,展示了如何使用Core Graphics框架绘制两条不同样式的线条,并介绍了图形上下文的保存与恢复机制。
#import "MyView.h"

@implementation MyView


- (void)drawRect:(CGRect)rect
{
    
    //图形上下文
     CGContextRef text = UIGraphicsGetCurrentContext();
    //第一条路线
    
    //保存上下文的状态
    CGContextSaveGState(text);
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointZero];
    [path addLineToPoint:CGPointMake(100, 100)];
    //设置显示样式
    CGContextSetLineWidth(text, 20.0);
    [[UIColor redColor] set];
    //将路径添加到图形上下文
    CGContextAddPath(text, path.CGPath);
    //渲染
    CGContextStrokePath(text);
    
    //第二条路线
    UIBezierPath *path2 = [UIBezierPath bezierPath];
    [path2 moveToPoint:CGPointMake(200, 200)];
    [path2 addLineToPoint:CGPointMake(300, 300)];
    //和下面两行的代码效果一样,清除原来的状态
    CGContextRestoreGState(text);
//    CGContextSetLineWidth(text, 1);
//    [[UIColor blackColor] set];
    CGContextAddPath(text, path2.CGPath);
    //渲染
    CGContextStrokePath(text);

    
}
@end


转载于:https://my.oschina.net/cgphp/blog/418609

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值