CoreGpaphics
CoreGpaphics基本应用
CGAffineTransformMake开头的函数 是基于最初始的位置来变化的
带有CGAffineTransform参数是基于CGAffineTransform的位置变化的
CGAffineTransformIdentify就是原来没有变化前的位置
主要函数有:
CG_EXTERN const CGAffineTransform CGAffineTransformIdentity
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0); /* Return the transform [ a b c d tx ty ]. */ CG_EXTERN CGAffineTransform CGAffineTransformMake(CGFloat a, CGFloat b,
CGFloat c, CGFloat d, CGFloat tx, CGFloat ty)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0); /* Return a transform which translates by `(tx, ty)':
t' = [ 1 0 0 1 tx ty ] */ CG_EXTERN CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx,
CGFloat ty) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0); /* Return a transform which scales by `(sx, sy)':
t' = [ sx 0 0 sy 0 0 ] */ CG_EXTERN CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0); /* Return a transform which rotates by `angle' radians:
t' = [ cos(angle) sin(angle) -sin(angle) cos(angle) 0 0 ] */ CG_EXTERN CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0); /* Return true if `t' is the identity transform, false otherwise. */ CG_EXTERN bool CGAffineTransformIsIdentity(CGAffineTransform t)
CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0); /* Translate `t' by `(tx, ty)' and return the result:
t' = [ 1 0 0 1 tx ty ] * t */ CG_EXTERN CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t,
CGFloat tx, CGFloat ty) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0); /* Scale `t' by `(sx, sy)' and return the result:
t' = [ sx 0 0 sy 0 0 ] * t */ CG_EXTERN CGAffineTransform CGAffineTransformScale(CGAffineTransform t,
CGFloat sx, CGFloat sy) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0); /* Rotate `t' by `angle' radians and return the result:
t' = [ cos(angle) sin(angle) -sin(angle) cos(angle) 0 0 ] * t */ CG_EXTERN CGAffineTransform CGAffineTransformRotate(CGAffineTransform t,
CGFloat angle) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0); /* Invert `t' and return the result. If `t' has zero determinant, then `t'
is returned unchanged. */ CG_EXTERN CGAffineTransform CGAffineTransformInvert(CGAffineTransform t)
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
重写drawRect 只能写在所有视图的父类 UIView,
UIImageView等视图是不允许重写的
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(50, 50) radius:50 startAngle:0 endAngle:2 * M_PI clockwise:YES];
CAShapeLayer *caShapeLayer = [CAShapeLayer layer];
caShapeLayer.path = bezierPath.CGPath;
caShapeLayer.fillColor = [UIColor redColor].CGColor;
[self.layer addSublayer:caShapeLayer];
NSString *attrString =@"hellohellohellohellohellohellohellohellohello";
UIColor *stringColor = [UIColor colorWithRed:1.0 green:0.0f blue:0 alpha:1.0]; //设置文本的颜色
NSDictionary* attrs =@{NSForegroundColorAttributeName:stringColor,
NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter"size:18],
}; //在词典中加入文本的颜色 字体 大小
[attrString drawInRect:CGRectMake(0,0,50,50)withAttributes:attrs]; //给文本限制个矩形边界,防止矩形拉伸;
NSString *s = @"我的小狗";
[s drawAtPoint:CGPointMake(100, 0) withFont:[UIFont systemFontOfSize:34.0]];
}
CGContextRef context= UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(context, CGRectMake(10, 100, 300, 280));
CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor);
CGContextFillPath(context); CGRect rectangle = CGRectMake(100, 290, 100, 25);
CGContextAddRect(context, rectangle);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextFillPath(context); CGContextBeginPath(context);
CGContextMoveToPoint(context, 160, 220);
CGContextAddLineToPoint(context, 190, 260);
CGContextAddLineToPoint(context, 130, 260);
CGContextClosePath(context);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextFillPath(context); CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextAddArc(context, 120, 170, 25, 0 * M_PI, 2 * M_PI, YES);
CGContextFillPath(context); CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextAddArc(context, 200, 170, 25, 0 * M_PI, 2 * M_PI, YES);
CGContextFillPath(context); CGContextBeginPath(context);
CGContextMoveToPoint(context, 160, 100);
CGContextAddQuadCurveToPoint(context, 160, 50, 190, 50);
CGContextSetLineCap(context, kCGLineCapSquare);
CGContextSetLineWidth(context, 20);
CGContextStrokePath(context);
CGContextSetStrokeColorWithColor(context, [UIColor brownColor].CGColor);
CGContextClosePath(context);
画图动画:
UIBezierPath *topBezierPath = [UIBezierPath bezierPath];
[topBezierPath moveToPoint:CGPointMake(160, 100)];
[topBezierPath addQuadCurveToPoint:CGPointMake(190,50) controlPoint:CGPointMake(160, 50)];
topBezierPath.lineCapStyle = kCGLineCapSquare;
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = topBezierPath.CGPath;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.strokeColor = [UIColor brownColor].CGColor;
shapeLayer.lineWidth = 20; CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
theAnimation.delegate = self;
theAnimation.duration = 2;
theAnimation.removedOnCompletion = FALSE;
theAnimation.fromValue = [NSNumber numberWithFloat:0];
theAnimation.toValue = [NSNumber numberWithFloat:2];
[shapeLayer addAnimation:theAnimation forKey:@"animateLayer"]; [self.layer addSublayer:shapeLayer];
白板画图:
通过一个简单的画板(白板)来熟悉 Quartz 2D绘图
添加一个Empty Application,新建一个UIView类
在AppDelegate中修改代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
//创建View,并指定View的大小为全屏
WhiteBoardView *whiteView = [[WhiteBoardView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[self.window addSubview:whiteView];//把创建的View作为子视图加入窗口
[whiteView release];
[self.window makeKeyAndVisible];
return YES;
}
修改UIView类中代码:
// WhiteBoardView.h
// DrawingBoard
#import <UIKit/UIKit.h>
@interface WhiteBoardView : UIView{
CGContextRef whiteBoardContext;
CGLayerRef whiteBoardLayer;
}
@end
实现类:
// WhiteBoardView.m
// DrawingBoard
#import "WhiteBoardView.h"
@implementation WhiteBoardView
//对进行重写,以便在视图初始化的时候创建并设置自定义的Context
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];//指定视图的背景色
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); //指定色彩空间为RGB
//创建Bitmap Context,大小为View自身的大小
whiteBoardContext = CGBitmapContextCreate(NULL, self.frame.size.width, self.frame.size.height, 8,
4 *self.frame.size.width, colorSpace, kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace) ;
//创建新的CGLayer,用于画图
whiteBoardLayer = CGLayerCreateWithContext(whiteBoardContext, self.frame.size, NULL);
//得到新建层的Context
CGContextRef layerContext = CGLayerGetContext(whiteBoardLayer);
//指定新建层Context的线宽
CGContextSetLineWidth(layerContext, 1.5);
CGContextSetLineCap(layerContext, kCGLineCapRound); //指定线头形状为圆形
CGContextSetRGBStrokeColor(layerContext, 0.0,0.0,0.0,1);//指定线的颜色,黑色
}
return self;
}
//对drawRect进行重写
- (void)drawRect:(CGRect)rect
{
//先得到当前的Context
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//根据Context的内容生成Bitmap
CGImageRef image = CGBitmapContextCreateImage(whiteBoardContext);
//把Bitmap绘制到Context上
CGContextDrawImage(currentContext, [self bounds], image);
//把whiteBoardLayer也绘到当前Context中
CGContextDrawLayerInRect(currentContext , [self bounds], whiteBoardLayer);
}
//屏幕触摸事件 触摸开始
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *theTouch = [touches anyObject];//先得到touch对象
if ([theTouch tapCount] == 2)//判断是否为双击,是就清空图像
{
CGContextClearRect(whiteBoardContext, [self bounds]);//清空
[self setNeedsDisplay]; //刷新屏幕显示
}
else//如果不是双击,就按手指划动处理,结果是画出一个点
{
[self touchesMoved:touches withEvent:event];
}
}
//手指划动时画线的代码
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *theTouch = [touches anyObject];
//得到当前位置
CGPoint currentTouchLocation = [theTouch locationInView:self];
//得到上次位置
CGPoint lastTouchLoacation = [theTouch previousLocationInView:self];
//取得Context
CGContextRef layerContext = CGLayerGetContext(whiteBoardLayer);
//开始定义Path
CGContextBeginPath(layerContext);
//把Path的起点移动到上次位置
CGContextMoveToPoint(layerContext, lastTouchLoacation.x, lastTouchLoacation.y);
//在上次位置和当前位置之间连线,并记入Path
CGContextAddLineToPoint(layerContext, currentTouchLocation.x, currentTouchLocation.y);
//沿Path画线
CGContextStrokePath(layerContext);
[self setNeedsDisplay]; //刷新屏幕
}
- (void)dealloc
{
CGContextRelease(whiteBoardContext);
CGLayerRelease(whiteBoardLayer);
[super dealloc];
}
@end
3.OpenGL ES编程
一般是三个步骤:
(1)在Context中绘图
(2)把Context中的内容送入framebuffer
(3)显示framebuffer
二.CGPath的使用
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGPathMoveToPoint(mutablePathRef, NULL, , );
//(10, 10) 起点, (50, 100) 中点, (100, 10)终点
CGPathAddCurveToPoint(mutablePathRef, NULL, , , , , , );
CGContextAddPath(contextRef, mutablePathRef);
CGContextSetStrokeColorWithColor(contextRef, [UIColor redColor].CGColor);
CGContextSetLineWidth(contextRef, );
CGContextDrawPath(contextRef, kCGPathFillStroke);
CoreGpaphics的更多相关文章
随机推荐
- luogu P2085 最小函数值
题目描述 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Ai*x^2+Bi*x+Ci (x∈N*).给定这些Ai.Bi和Ci,请求出所有函数的所有函数值中最小的m个(如有重复的要输出多个 ...
- 利用BURPSUITE检测CSRF漏洞
CSRF漏洞的手动判定:修改referer头或直接删除referer头,看在提交表单时,网站是否还是正常响应. 下面演示用Burpsuite对CSRF进行鉴定. 抓包. 成功修改密码完成漏洞的利用.
- easyui combobox模糊查询
用easyui框架开发的攻城狮恐怕都遇到过这样一个问题,就是在新增页面combobox下拉框需要支持模糊查询,但是输入不是combobox中Data里面的值的时候,点击保存,依然是可以新增进去的,这样 ...
- iOS数据持久化存储
本文中的代码托管在github上:https://github.com/WindyShade/DataSaveMethods 相对复杂的App仅靠内存的数据肯定无法满足,数据写磁盘作持久化存储是几乎每 ...
- 浅谈Heatmap
在自然界之中,蛇的眼睛有夜视功能,即便是茫茫黑夜,它也能轻而易举的找到猎物,这是因为任何物体都会辐射热红外,且辐射的高低和温度成正比,由于生命体的体温会明显高于周围环境的温度,所以在蛇眼面前便无处遁形 ...
- weex 项目开发(一) weex create project 与 weex init project 的区别
开发环境配置:http://www.cnblogs.com/crazycode2/p/7822961.html 1. weex create project 与 weex init project ...
- MongoDB---出现no write has been done on this connection解决方式
no write has been done on this connection 这个问题出现了好几天.日志里面一天出现几十次no write has been done on this conne ...
- C#压缩或解压(rar和zip文件)
/// <summary> /// 解压RAR和ZIP文件(需存在Winrar.exe(只要自己电脑上可以解压或压缩文件就存在Winrar.exe)) /// </summary&g ...
- 为什么说JAVA中要慎重使用继承 C# 语言历史版本特性(C# 1.0到C# 8.0汇总) SQL Server事务 事务日志 SQL Server 锁详解 软件架构之 23种设计模式 Oracle与Sqlserver:Order by NULL值介绍 asp.net MVC漏油配置总结
为什么说JAVA中要慎重使用继承 这篇文章的主题并非鼓励不使用继承,而是仅从使用继承带来的问题出发,讨论继承机制不太好的地方,从而在使用时慎重选择,避开可能遇到的坑. JAVA中使用到继承就会有两 ...
- iOS 开发之 - 关闭键盘 退出键盘 的5种方式
iOS 开发之 - 关闭键盘 退出键盘 的5种方式 1.点击编辑区以外的地方(UIView) 2.点击编辑区域以外的地方(UIControl) 3.使用制作收起键盘的按钮 4.使用判断输入字元 5 ...