PDFKTColorPickView.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // PDFColorPickView.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "PDFKTColorPickView.h"
  11. @implementation PDFKTColorPickView
  12. {
  13. NSMutableArray *_colors;
  14. UIView *_pickColor;
  15. UIImageView *_pickColorBack;
  16. }
  17. @synthesize colorPickDelegate = _colorPickDelegate;
  18. - (id)initWithFrame:(CGRect)frame
  19. {
  20. self = [super initWithFrame:frame];
  21. if (self) {
  22. // Initialization code
  23. _colors = [[NSMutableArray alloc]init];
  24. [self initColors];
  25. UIImage *pickImg = [UIImage imageNamed:@"color_view.png"];
  26. _pickColorBack = [[UIImageView alloc]initWithImage:pickImg];
  27. _pickColorBack.frame = CGRectMake(0, -70,pickImg.size.width,pickImg.size.height);
  28. [self addSubview:_pickColorBack];
  29. _pickColorBack.hidden = YES;
  30. _pickColor = [[UIView alloc] initWithFrame:CGRectMake(3, 3, 38, 38)];
  31. _pickColor.backgroundColor = [UIColor greenColor];
  32. _pickColor.layer.borderColor = [[UIColor grayColor] CGColor];
  33. _pickColor.layer.borderWidth = 0.7f;
  34. [_pickColorBack addSubview:_pickColor];
  35. // _pickColor.hidden = YES;
  36. }
  37. return self;
  38. }
  39. - (void)dealloc
  40. {
  41. _colorPickDelegate = nil;
  42. [_colors release];
  43. [_pickColor release];
  44. [_pickColorBack release];
  45. [super dealloc];
  46. }
  47. - (void)initColors
  48. {
  49. UIColor *color = [UIColor colorWithRed:219/255.0 green:3/255.0 blue:1/255.0 alpha:1];
  50. [_colors addObject:color];
  51. color = [UIColor colorWithRed:255/255.0 green:127/255.0 blue:0/255.0 alpha:1];
  52. [_colors addObject:color];
  53. color = [UIColor colorWithRed:253/255.0 green:135/255.0 blue:106/255.0 alpha:1];
  54. [_colors addObject:color];
  55. color = [UIColor colorWithRed:254/255.0 green:218/255.0 blue:28/255.0 alpha:1];
  56. [_colors addObject:color];
  57. color = [UIColor colorWithRed:87/255.0 green:218/255.0 blue:20/255.0 alpha:1];
  58. [_colors addObject:color];
  59. color = [UIColor colorWithRed:41/255.0 green:109/255.0 blue:209/255.0 alpha:1];
  60. [_colors addObject:color];
  61. color = [UIColor colorWithRed:207/255.0 green:27/255.0 blue:255/255.0 alpha:1];
  62. [_colors addObject:color];
  63. color = [UIColor colorWithRed:254/255.0 green:28/255.0 blue:133/255.0 alpha:1];
  64. [_colors addObject:color];
  65. color = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1];
  66. [_colors addObject:color];
  67. color = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1];
  68. [_colors addObject:color];
  69. // color = [UIColor colorWithRed:18/255.0 green:17/255.0 blue:77/255.0 alpha:1];
  70. // [_colors addObject:color];
  71. // color = [UIColor colorWithRed:78/255.0 green:24/255.0 blue:116/255.0 alpha:1];
  72. // [_colors addObject:color];
  73. // color = [UIColor colorWithRed:128/255.0 green:30/255.0 blue:113/255.0 alpha:1];
  74. // [_colors addObject:color];
  75. // color = [UIColor colorWithRed:224/255.0 green:43/255.0 blue:95/255.0 alpha:1];
  76. // [_colors addObject:color];
  77. // color = [UIColor colorWithRed:87/255.0 green:43/255.0 blue:15/255.0 alpha:1];
  78. // [_colors addObject:color];
  79. // color = [UIColor colorWithRed:71/255.0 green:69/255.0 blue:70/255.0 alpha:1];
  80. // [_colors addObject:color];
  81. // color = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
  82. // [_colors addObject:color];
  83. // color = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
  84. // [_colors addObject:color];
  85. }
  86. - (void)pick:(CGPoint)location show:(BOOL)show
  87. {
  88. CGFloat tPosX = location.x;
  89. CGFloat cellWidth = CGRectGetWidth(self.bounds)/ (CGFloat) [_colors count];
  90. if (tPosX < 0) tPosX = 0.0;
  91. NSUInteger index = (NSUInteger)(tPosX / cellWidth);
  92. if (index > [_colors count] - 1) index = [_colors count] - 1;
  93. _pickColor.backgroundColor = [_colors objectAtIndex:index];
  94. CGFloat tPosY = _pickColorBack.center.y;
  95. if (tPosX > self.bounds.size.width) tPosX = self.bounds.size.width;
  96. if (tPosX < 0) tPosX = 0;
  97. _pickColorBack.center = CGPointMake(tPosX, tPosY);
  98. _pickColorBack.hidden = !show;
  99. if (_colorPickDelegate && [_colorPickDelegate respondsToSelector:@selector(colorPick:updateColor:)])
  100. {
  101. [_colorPickDelegate colorPick:self updateColor:_pickColor.backgroundColor];
  102. }
  103. }
  104. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  105. {
  106. UITouch *touch = [touches anyObject];
  107. CGPoint location = [touch locationInView:self];
  108. [self pick:location show:YES];
  109. [self.colorPickDelegate colorPickDidBegan:self];
  110. }
  111. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  112. {
  113. // 检查point是否改变
  114. CGPoint cPoint = [[touches anyObject] locationInView:self];
  115. CGPoint pPoint = [[touches anyObject] previousLocationInView:self];
  116. if (CGPointEqualToPoint(cPoint, pPoint)) {
  117. return;
  118. }
  119. UITouch *touch = [touches anyObject];
  120. CGPoint location = [touch locationInView:self];
  121. [self pick:location show:YES];
  122. }
  123. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  124. {
  125. UITouch *touch = [touches anyObject];
  126. CGPoint location = [touch locationInView:self];
  127. [self pick:location show:NO];
  128. [self.colorPickDelegate colorPickDidEnd:self];
  129. }
  130. @end