iOS涂鸦-自由画笔可以用到的代码
2016-03-17 15:06
148 查看
1.线段模用来存储线段信息
// // Line.h // 线段模型 #import <UIKit/UIKit.h> @interface Line : NSObject /** * 线条所包含的所有点 */ @property (nonatomic,strong)NSMutableArray <__kindof NSValue *>*linePoints; /** * 线条的颜色 */ @property (nonatomic,strong)UIColor *lineColor; /** * 线条的粗细 */ @property (nonatomic,assign)NSInteger lineWidth; @end
#import "Line.h" @implementation Line - (instancetype)init { if (self=[super init]) { self.linePoints = [NSMutableArray array]; } return self; } @end2.自定义控件
// // DrawLine.h // 创建一个直线 #import <UIKit/UIKit.h> @interface DrawLine : UIView /** * 线的颜色(用于接收外部传进来的颜色) */ @property (nonatomic, strong) UIColor *lineColor; /** * 线宽(用于接收外部传进来) */ @property (nonatomic, assign) NSInteger lineWidth; @end
// // DrawLine.m #import "DrawLine.h" #import "Line.h" @interface DrawLine() /** * 存放所有线条的信息(颜色、坐标、宽度) */ @property(nonatomic,strong)NSMutableArray *allLineInfos; @end @implementation DrawLine - (NSMutableArray *)lineInfos { if (_allLineInfos == nil) { _allLineInfos = [NSMutableArray array]; } return _allLineInfos; } /** * 从代码创建控件会调用(这个方法里做一次性初始化设置) */ - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor clearColor]; self.lineWidth = 2; self.lineColor = [UIColor redColor]; } return self; } #pragma mark - touche /** * 开始触摸 */ - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // 1.取出UITouch对象 UITouch *touch=[touches anyObject]; // 2.创建一个线条信息模型 Line *info = [Line new]; info.lineColor = self.lineColor; info.lineWidth = self.lineWidth; [info.linePoints addObject:[NSValue valueWithCGPoint:[touch locationInView:self]]]; // 3.把该线条信息模型添加到数组 [self.allLineInfos addObject:info]; // 4.重绘 [self setNeedsDisplay]; } /** * 触摸点一直在移动就会调用 */ - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // 1.取出所有point NSArray* MovePointArray=[touches allObjects]; // 2.取出最新添加的一个线段信息模型 Line *lastInfo = [self.lineInfos lastObject]; // 3.给这个模型的linePoints属性添加值 [lastInfo.linePoints addObject:[NSValue valueWithCGPoint:[[MovePointArray objectAtIndex:0] locationInView:self]]]; // 4.重绘 [self setNeedsDisplay]; } #pragma mark - 绘制 /** * 根据现有的线条绘制相应的图形 */ - (void)drawRect:(CGRect)rect { CGContextRef context=UIGraphicsGetCurrentContext(); CGContextSetLineCap(context, kCGLineCapRound); CGContextSetLineJoin(context, kCGLineJoinRound); if (self.allLineInfos.count>0) { for (int i=0; i<[self.allLineInfos count]; i++) { Line *info = self.allLineInfos[i]; CGContextBeginPath(context); CGPoint myStartPoint=[[info.linePoints objectAtIndex:0] CGPointValue]; CGContextMoveToPoint(context, myStartPoint.x, myStartPoint.y); if (info.linePoints.count>1) { for (int j=0; j<[info.linePoints count]-1; j++) { CGPoint myEndPoint=[[info.linePoints objectAtIndex:j+1] CGPointValue]; CGContextAddLineToPoint(context, myEndPoint.x,myEndPoint.y); } }else { CGContextAddLineToPoint(context, myStartPoint.x,myStartPoint.y); } // CGContextSetStrokeColorWithColor(context, info.lineColor.CGColor); CGContextSetLineWidth(context, info.lineWidth); // 定义阴影 NSShadow* shadow4 = [[NSShadow alloc] init]; [shadow4 setShadowColor: info.lineColor]; [shadow4 setShadowOffset: CGSizeMake(0.0, 0.0)]; [shadow4 setShadowBlurRadius: info.lineWidth * 1.2]; // 绘制阴影 CGContextSaveGState(context); CGContextSetShadowWithColor(context, shadow4.shadowOffset, shadow4.shadowBlurRadius, [shadow4.shadowColor CGColor]); [[UIColor colorWithRed:245/255.0 green:255/255.0 blue:243/255.0 alpha:1.0] setStroke]; CGContextStrokePath(context); } } } @end3.控制器里如何使用?
DrawLine *line = [[DrawLine alloc]initWithFrame:CGRectMake(0, 0, screenW, screenH)]; line.lineWidth = self.currentWidth; line.lineColor = self.currentColor; [self.bgView addSubview:line];
相关文章推荐
- 使用Xcode7的Instruments检测解决iOS内存泄露
- iOS之界面传值(通知,属性,协议,NSUserDefaults,KVC)
- ARC 内存管理解惑
- 出来ios顶部导航掉下来问题
- 你最喜欢的 iOS 开发的技巧和提示有哪些?
- iOS横屏处理
- ios代理设计模式
- iOS乱码
- iOS 百度地图定位不显示蓝色小圆圈
- (转)iOS 上的相机捕捉
- iOS关于AutoLayout和AutoResizing
- iOS 与H5交互
- 七牛------iOS多文件上传demo
- IOS中添加语音播放文字
- IOS 把格式化的JSON字符串转换成字典
- iOS模态跳转包装navigation导航栏
- IOS集成到支付宝的步骤及问题--oschina
- iOS tableview自定制以及scrollview的无卡顿定时滚动
- iOS数据存储之属性列表理解
- 控制器瘦身-代理对象