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;
}
@end
2.自定義控件
//
// 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);
}
}
}
@end
3.控制器裡如何使用?
DrawLine *line = [[DrawLine alloc]initWithFrame:CGRectMake(0, 0, screenW, screenH)];
line.lineWidth = self.currentWidth;
line.lineColor = self.currentColor;
[self.bgView addSubview:line];