HINSPRecordButton.m 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // TWRecordButton.m
  3. // TT
  4. //
  5. // Created by Christian Menschel on 18.08.14.
  6. // Copyright (c) 2014 tapwork. All rights reserved.
  7. //
  8. #import "HINSPRecordButton.h"
  9. @interface HINSPRecordButton ()
  10. @property (nonatomic, weak) CAShapeLayer *shapeLayer;
  11. @end
  12. @implementation HINSPRecordButton
  13. - (instancetype)initWithFrame:(CGRect)frame
  14. {
  15. self = [super initWithFrame:frame];
  16. if (self) {
  17. [self addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
  18. }
  19. return self;
  20. }
  21. #pragma mark - Actions
  22. - (void)tapped:(id)sender
  23. {
  24. self.isRecording = !self.isRecording;
  25. }
  26. - (void)setIsRecording:(BOOL)isRecording {
  27. if (_isRecording != isRecording) {
  28. _isRecording = isRecording;
  29. UIColor *color = nil;
  30. if (isRecording) {
  31. color = [self recordingColor];
  32. } else {
  33. color = [self defaultColor];
  34. }
  35. _shapeLayer.fillColor = color.CGColor;
  36. }
  37. }
  38. #pragma mark - Setter
  39. - (void)setFrame:(CGRect)frame
  40. {
  41. [super setFrame:frame];
  42. CAShapeLayer *shapeLayer = (CAShapeLayer*)self.layer;
  43. UIColor *color = [self defaultColor];
  44. shapeLayer.fillColor = color.CGColor;
  45. CGRect rect = CGRectMake(0.0f, 0.0f, frame.size.width,frame.size.height);
  46. CGPathRef path = CGPathCreateWithEllipseInRect(rect, NULL);
  47. shapeLayer.path = path;
  48. CGPathRelease(path);
  49. _shapeLayer = shapeLayer;
  50. }
  51. - (UIColor*)defaultColor
  52. {
  53. if (self.isRecording) {
  54. return [UIColor redColor];
  55. }
  56. return [UIColor grayColor];
  57. }
  58. - (UIColor*)recordingColor
  59. {
  60. return [UIColor redColor];
  61. }
  62. #pragma mark - Class methdods
  63. + (Class)layerClass {
  64. return [CAShapeLayer class];
  65. }
  66. @end