iOS Quartz2D 常用方法总结
来源:互联网 发布:java 转换base64编码 编辑:程序博客网 时间:2024/05/20 06:54
// 这里是Layer Graphics Context
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 绘制图形
// 设置起点
CGContextMoveToPoint(ctx, 10, 10);
// 设置终点 ctx 是上下文
CGContextAddLineToPoint(ctx, 10, 100);
// 绘制图形(渲染到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, 50, 0);
// 设置第二个点
CGContextAddLineToPoint(ctx, 0, 100);
// 设置第三个点
CGContextAddLineToPoint(ctx, 100, 100);
// 进行封口操作
CGContextClosePath(ctx);
// 渲染
CGContextStrokePath(ctx);
CGContextAddRect(ctx, CGRectMake(50, 100, 50, 100));
[[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, 10, 10);
// 设置终点 ctx 是上下文
CGContextAddLineToPoint(ctx, 10, 100);
CGContextAddLineToPoint(ctx, 100, 100);
// 设置绘图状态
// 设置线条颜色
CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1.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.0, 200),CGPointMake(50.0, 100),CGPointMake(90.0, 200),CGPointMake(130.0, 100),CGPointMake(170.0, 200),CGPointMake(210.0, 100)};
// 绘制
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(120, 260);
endPoint = CGPointMake(200.0, 200);
CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, 0);
}
图片的填充
- (void)drawRect:(CGRect)rect {
UIImage * image = [UIImage imageNamed:@"abc"];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect re = CGRectMake(60, 60, 60, 60);
CGContextClipToRect(ctx, CGRectMake(0, 0, 320, 480));
CGContextDrawTiledImage(ctx, re, image.CGImage);
}
- iOS Quartz2D 常用方法总结
- iOS---常用方法总结
- iOS常用方法总结
- 【iOS开发-quartz2d】关于drwRect方法
- ios字符串常用方法总结
- iOS GCD常用方法总结
- iOS开发常用方法总结
- iOS NSMutableAttributedString常用方法总结
- IOS:Quartz2D
- iOS-Quartz2D
- iOS-Quartz2D
- Quartz2d总结
- Quartz2D方法
- iOS-Quartz2D绘图的基础用法总结
- iOS之UIView常用方法总结
- iOS笔记-(ASI缓存常用方法总结)
- IOS UITextField常用的代理方法总结
- iOS UIlabel一些常用属性方法总结
- Thinkphp3.1.3学习——1、安装框架
- Mysql 取整函数
- 【Scheme归纳】6 赋值
- AppStore 2015年3月3日 新审核条款
- 微信企业号回调模式配置详解
- iOS Quartz2D 常用方法总结
- NIO系列6:流行 NIO Framework netty 和 mina 性能测评与分析
- DbContext-> Dispose demo
- 快速检测Javascript的理解深度
- 【转载】HTTP协议与HTML表单(再谈GET与POST的区别)
- JAVA 面向对象 基础概念,匿名对象的使用,方法的重载
- 怎样强制修改笔记本密码
- [CSDN_Markdown] 使用LaTeX写矩阵
- tomcat 性能之谜