猎豆优选

ZLDrawItem.m 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // ZLDrawItem.m
  3. // ZLPhotoBrowser
  4. //
  5. // Created by long on 2018/5/9.
  6. // Copyright © 2018年 long. All rights reserved.
  7. //
  8. #import "ZLDrawItem.h"
  9. @interface ZLDrawItem ()
  10. {
  11. ZLDrawItemColorType _type;
  12. UIView *_colorView;
  13. }
  14. @end
  15. @implementation ZLDrawItem
  16. - (instancetype)initWithFrame:(CGRect)frame
  17. {
  18. return [self initWithFrame:CGRectZero colorType:ZLDrawItemColorTypeWhite target:nil action:nil];
  19. }
  20. - (instancetype)initWithCoder:(NSCoder *)coder
  21. {
  22. return [self initWithFrame:CGRectZero colorType:ZLDrawItemColorTypeWhite target:nil action:nil];
  23. }
  24. - (instancetype)initWithFrame:(CGRect)frame colorType:(ZLDrawItemColorType)colorType target:(id)target action:(SEL)action
  25. {
  26. if (self = [super initWithFrame:frame]) {
  27. UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
  28. [self addGestureRecognizer:gesture];
  29. _type = colorType;
  30. _colorView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.bounds)-10, CGRectGetMidY(self.bounds)-10, 20, 20)];
  31. _colorView.layer.masksToBounds = YES;
  32. _colorView.layer.cornerRadius = 10;
  33. _colorView.backgroundColor = [self colorWithType:colorType];;
  34. [self addSubview:_colorView];
  35. }
  36. return self;
  37. }
  38. - (UIColor *)color
  39. {
  40. return [self colorWithType:_type];
  41. }
  42. - (UIColor *)colorWithType:(ZLDrawItemColorType)colorType
  43. {
  44. switch (colorType) {
  45. case ZLDrawItemColorTypeWhite: return [UIColor whiteColor];
  46. case ZLDrawItemColorTypeDarkGray: return [UIColor darkGrayColor];
  47. case ZLDrawItemColorTypeRed: return [UIColor redColor];
  48. case ZLDrawItemColorTypeYellow: return [UIColor yellowColor];
  49. case ZLDrawItemColorTypeGreen: return [UIColor greenColor];
  50. case ZLDrawItemColorTypeBlue: return [UIColor blueColor];
  51. case ZLDrawItemColorTypePurple: return [UIColor purpleColor];
  52. default: return [UIColor redColor];
  53. }
  54. }
  55. - (void)setSelected:(BOOL)selected
  56. {
  57. _selected = selected;
  58. if (selected) {
  59. [UIView animateWithDuration:0.15 animations:^{
  60. _colorView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.2, 1.2);
  61. }];
  62. } else {
  63. _colorView.transform = CGAffineTransformIdentity;
  64. }
  65. }
  66. @end