iOS涂鸦-自由画笔可以用到的代码
来源:互联网 发布:linux 定时器状态 编辑:程序博客网 时间:2024/05/16 02:07
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];
0 0
- iOS涂鸦-自由画笔可以用到的代码
- 收集各种 iOS App 开发可以用到的代码示例
- 收集各种 iOS App 开发可以用到的代码示例
- 收集各种 iOS App 开发可以用到的代码示例
- js可以用到的代码
- canvas画笔(涂鸦)
- iOS 经常用到的代码
- Android屏幕上涂鸦画笔标记的实现
- 另类感的涂鸦代码
- iOS开发封装篇-利用绘图封装一个可以涂鸦的view
- <未完>iOS开发可以用到的mac命令
- 关于创建涂鸦UI以及保存涂鸦图片的代码
- ios 涂鸦
- iphone ios 开发常用代码(我用到的)
- Cache客户端代码已经在googlecode的svn上建立,同学们可以自由下载
- xss用到的代码
- 【Android】自定义View、画家(画布)Canvas与画笔Paint的应用——画图、涂鸦板app的实现
- ios 用到的数据
- Oracle数据库开发和设计规范
- mysql索引问题
- Redis开源代码读书笔记二(源代码及工程结构)
- WEB学习笔记
- JavaBean简介
- iOS涂鸦-自由画笔可以用到的代码
- itextPDF生成PDF
- 重写strcpy
- 成为JavaGC专家(1)
- Mac下valgrind的安装和使用
- IOS异步获取数据并刷新界面dispatch_async的使用方法
- Picasso加载图片
- 给gridview子项设置宽高考虑屏幕适配
- 蓝桥杯 概率计算(DP)