您的位置:首页 > 移动开发 > IOS开发

ios 画图 绘图的实现(自由批注中视图的subview的实现)bitmap Quartz 2D 缓存位图 自由批注功能的实现 如何实现在view上自由绘图 提供源码

2012-03-06 14:15 1026 查看
代码结构:

一。在视图控制器(viewController)中实现绘图视图中控件的实现方法,比如;选择颜色,选择橡皮,选择线条粗细等等,方法调用视图控制器所绑定的view的方法,

二。在视图中(view)加入触摸的起始点、终点、代理,线条粗细,颜色的成员并重写

- (void)drawRect:(CGRect)rect

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
方法中相关的方法委托代理进行实现。例如touchMoved的代码

SCStrokeImp* stroke2 = (SCStrokeImp*)strokeDelegate;//先对代理类的成员进行强制类型转换,以便能识别其中的方法
stroke2.flag =self.flag; //橡皮实现中的flag判断是绘制还是擦除
UITouch *touch = [touchesanyObject];
lastTouch = [touchlocationInView:self]; //获取mov终点坐标

[strokeDelegatestrokeView:selfdrawLineFrom:firstTouchandTo:lastTouch];//把绘图的实现扔给代理类进行实现
firstTouch =lastTouch; //终点=起点,准备绘制下一条线段
[selfsetNeedsDisplay]; //对绘制的图形进行展示

三。在代理类中实现的方法
-(CGContextRef)CreateBitmapContextwithWidth:(int)aWidth
andHeight:(int)aHeight; //创建一个缓存位图,实现对划线的记忆
-(void)strokeView:(SCStrokeView*)aView (1)
drawLineFrom:(CGPoint)aStart
andTo:(CGPoint)aEnd; //视图调用的划线方法,含有被代理者本身作为参数,以便获得被代理者当前的状态
-(void)updateView:(SCStrokeView*)aView withContext:(CGContextRef) context; //更新当前view
- (void) drawLineFrom:(CGPoint)aStart andTo:(CGPoint)aEnd; //1获取到位置,线条宽度和颜色以后调用的实际划线的方法
- (CGFloat)getStepWidth:(CGFloat)aBeginWidth
andEndWidth:(CGFloat)aEndWidth
andDistance:(CGFloat)aDistance
andStepLength:(CGFloat)aLength;
- (CGFloat)getDistanceFrom: (CGPoint)aStart
andTo: (CGPoint)aEnd;
- (NSMutableArray*)getPointSetWithFrom:(CGPoint)aStart
andTo:(CGPoint)aEnd;
- (CGFloat)getLineSlopeWithFrom:(CGPoint)aStart
andTo:(CGPoint)aEnd; //这四个函数负责对线条进行优化,消除锯齿
四。在接口中声明的方法(代理服从改接口)
-(void)strokeView:(SCStrokeView*)aView
drawLineFrom:(CGPoint)aStart
andTo:(CGPoint)aEnd;
-(void)updateView:(SCStrokeView*)aView withContext:(CGContextRef) context;
//这两个方法在view中被调用,所以想要成为view的代理,必须实现这两个方法

关于自由批注的其他代码 参考我其他的帖子 需要源码请留言

代码地址
http://dl.dbank.com/c0gys4jhay
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐