//
// 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;
}
@end
//
// 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 *)allLineInfos
{
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.allLineInfos 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);
}
}
}
@end
DrawLine *line = [[DrawLine alloc]initWithFrame:CGRectMake(0, 0, screenW, screenH)];
line.lineWidth = self.currentWidth;
line.lineColor = self.currentColor;
[self.bgView addSubview:line];
网友评论