图形上下文
来源:互联网 发布:软件负载均衡技术 编辑:程序博客网 时间:2024/05/17 08:34
实现方式1:
- (void)drawRect:(CGRect)rect{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.拼接路径(绘图的信息)
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10, 125)];
[path addLineToPoint:CGPointMake(240, 125)];
// 3.路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
// 设置绘图的状态
[[UIColor redColor] set];
CGContextSetLineWidth(ctx, 10);
CGContextSetLineCap(ctx, kCGLineCapRound);
// 4.渲染
CGContextStrokePath(ctx);
// 第二根线(需要再拼接路径,否则因为是一条路径会出现另一种吓尿了的效果)UIBezierPath *path1 = [UIBezierPath bezierPath];[path1 moveToPoint:CGPointMake(125, 10)];[path1 addLineToPoint:CGPointMake(125, 240)];CGContextAddPath(ctx, path1.CGPath);// 4.渲染(分开渲染,否则颜色等状态无法分别设置)CGContextStrokePath(ctx);
}
实现方式2:
- (void)drawRect:(CGRect)rect{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.把ctx拷贝一份放在栈中
CGContextSaveGState(ctx);
// 3.拼接路径(绘图的信息)
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10, 125)];
[path addLineToPoint:CGPointMake(240, 125)];
// 4.路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
// 设置绘图的状态
[[UIColor redColor] set];
CGContextSetLineWidth(ctx, 10);
CGContextSetLineCap(ctx, kCGLineCapRound);
// 5.渲染
CGContextStrokePath(ctx);
// 第二根线
UIBezierPath *path1 = [UIBezierPath bezierPath];
[path1 moveToPoint:CGPointMake(125, 10)];
[path1 addLineToPoint:CGPointMake(125, 240)];
CGContextAddPath(ctx, path1.CGPath);
// 6. 把栈顶上下文取出来,替换当前上下文
CGContextRestoreGState(ctx);
// 7.渲染
CGContextStrokePath(ctx);
}
- 图形上下文
- 图形设备/图形上下文
- 图形上下文栈
- 32.图形上下文
- 37.图形上下文栈
- Quarzt2D--图形上下文栈
- Quartz2D-图形上下文栈
- ios图形上下文CGContext
- Quartz2D:图形上下文栈
- iOS - 图形上下文使用
- 深入理解图形上下文
- 图形上下文栈
- ios-图形上下文栈
- Symbian中的图形设备和图形上下文
- 处理图形上下文中的文体
- ios UIKit的图形上下文
- 图形上下文解释CGContextSaveGState/CGContextRestoreGState
- 进行图形上下文画虚线
- 简单回调机制的基本建立
- ReactiveCocoa完整demo教程No.1
- 详解javascript之delete操作符
- PHPwind9.01傻瓜图解安装教程
- 通过JAVA代码获取手机的一些基本信息(本机号码,SDK版本,系统版本,手机型号)
- 图形上下文
- redis上tomcat和session共享的问题
- Android RSA加密解密
- 了解DMA技术
- 类似桌面背景壁纸随手指滑动--第三方开源--BackgroundViewPager
- 升级Python版本脚本
- EF学习笔记——生成自定义实体类
- phpwind图文安装2
- 引导界面(只显示一次)的实现