悟空记账

XHSScreenshotDrawView.m 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // XHSScreenshotDrawView.m
  3. // XinHuaShe
  4. //
  5. // Created by 王强 on 2017/12/20.
  6. // Copyright © 2017年 xinhuanmm. All rights reserved.
  7. //
  8. #import "XHSScreenshotDrawView.h"
  9. #import "XHSScreenshotFingerDrawLineModel.h"
  10. @implementation XHSScreenshotDrawView
  11. - (id)initWithFrame:(CGRect)frame {
  12. self = [super initWithFrame:frame];
  13. if (self) {
  14. [self setupView];
  15. }
  16. return self;
  17. }
  18. #pragma mark - setupView
  19. - (void)setupView {
  20. // 默认值设置
  21. self.lineWidth = 3.0;
  22. self.lineColor = [UIColor redColor];
  23. self.backgroundColor = [UIColor clearColor];
  24. self.allDrawPaletteLineArray = [NSMutableArray new];
  25. }
  26. #pragma mark - 涂鸦
  27. //根据现有的线条,绘制相应的图画
  28. - (void)drawRect:(CGRect)rect {
  29. CGContextRef context=UIGraphicsGetCurrentContext();
  30. CGContextSetLineCap(context, kCGLineCapRound);
  31. CGContextSetLineJoin(context, kCGLineJoinRound);
  32. if (self.allDrawPaletteLineArray.count) {
  33. for (int i=0; i<[self.allDrawPaletteLineArray count]; i++) {
  34. XHSScreenshotFingerDrawLineModel *info = self.allDrawPaletteLineArray[i];
  35. CGContextBeginPath(context);
  36. CGPoint myStartPoint=[[info.linePoints objectAtIndex:0] CGPointValue];
  37. CGContextMoveToPoint(context, myStartPoint.x, myStartPoint.y);
  38. if (info.linePoints.count>1) {
  39. for (int j=0; j<[info.linePoints count]-1; j++) {
  40. CGPoint myEndPoint=[[info.linePoints objectAtIndex:j+1] CGPointValue];
  41. CGContextAddLineToPoint(context, myEndPoint.x,myEndPoint.y);
  42. }
  43. }else {
  44. CGContextAddLineToPoint(context, myStartPoint.x,myStartPoint.y);
  45. }
  46. CGContextSetStrokeColorWithColor(context, info.lineColor.CGColor);
  47. CGContextSetLineWidth(context, info.lineWidth+1);
  48. CGContextStrokePath(context);
  49. }
  50. }
  51. }
  52. #pragma mark - touchesBegan代理
  53. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  54. //在触摸开始的时候,添加一条新的线条并初始化
  55. UITouch* touch=[touches anyObject];
  56. XHSScreenshotFingerDrawLineModel *info = [XHSScreenshotFingerDrawLineModel new];
  57. info.lineColor = self.lineColor;
  58. info.lineWidth = self.lineWidth;
  59. [info.linePoints addObject:[NSValue valueWithCGPoint:[touch locationInView:self]]];
  60. [self.allDrawPaletteLineArray addObject:info];
  61. [self setNeedsDisplay];
  62. }
  63. #pragma mark - touchesMoved代理
  64. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  65. //在触摸移动的时候,将现有的线条的最后一条的point增加相应的触摸过的坐标
  66. NSArray* MovePointArray=[touches allObjects];
  67. XHSScreenshotFingerDrawLineModel *lastInfo = [self.allDrawPaletteLineArray lastObject];
  68. [lastInfo.linePoints addObject:[NSValue valueWithCGPoint:[[MovePointArray objectAtIndex:0] locationInView:self]]];
  69. [self setNeedsDisplay];
  70. }
  71. #pragma mark - 清空涂鸦
  72. - (void)cleaAllrDrawing {
  73. if ([self.allDrawPaletteLineArray count]>0) {
  74. [self.allDrawPaletteLineArray removeAllObjects];
  75. [self setNeedsDisplay];
  76. }
  77. }
  78. #pragma mark - 撤销涂鸦
  79. - (void)cleanPreviousDraw {
  80. if ([self.allDrawPaletteLineArray count]>0) {
  81. [self.allDrawPaletteLineArray removeLastObject];
  82. }
  83. [self setNeedsDisplay];
  84. }
  85. @end