SCLTimerDisplay.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // SCLTimerDisplay.m
  3. // SCLAlertView
  4. //
  5. // Created by Taylor Ryan on 8/18/15.
  6. // Copyright (c) 2015-2017 AnyKey Entertainment. All rights reserved.
  7. //
  8. #import "SCLTimerDisplay.h"
  9. #import "SCLMacros.h"
  10. @interface SCLTimerDisplay ()
  11. @property (strong, nonatomic) UILabel *countLabel;
  12. @end
  13. @implementation SCLTimerDisplay
  14. @synthesize currentAngle;
  15. - (instancetype)initWithFrame:(CGRect)frame
  16. {
  17. self = [super initWithFrame:frame];
  18. if (self)
  19. {
  20. self.backgroundColor = [UIColor clearColor];
  21. currentAngle = 0.0f;
  22. }
  23. return self;
  24. }
  25. - (instancetype)initWithOrigin:(CGPoint)origin radius:(CGFloat)r
  26. {
  27. return [self initWithOrigin:(CGPoint)origin radius:r lineWidth:5.0f];
  28. }
  29. - (instancetype)initWithOrigin:(CGPoint)origin radius:(CGFloat)r lineWidth:(CGFloat)width
  30. {
  31. self = [super initWithFrame:CGRectMake(origin.x, origin.y, r*2, r*2)];
  32. if (self) {
  33. self.backgroundColor = [UIColor clearColor];
  34. currentAngle = START_DEGREE_OFFSET;
  35. radius = r-(width/2);
  36. lineWidth = width;
  37. self.color = [UIColor whiteColor];
  38. self.userInteractionEnabled = NO;
  39. // Add count label
  40. _countLabel = [[UILabel alloc] init];
  41. _countLabel.textColor = [UIColor whiteColor];
  42. _countLabel.backgroundColor = [UIColor clearColor];
  43. _countLabel.font = [UIFont fontWithName: @"HelveticaNeue-Bold" size:12.0f];
  44. _countLabel.textAlignment = NSTextAlignmentCenter;
  45. _countLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  46. [self addSubview:_countLabel];
  47. }
  48. return self;
  49. }
  50. - (void)updateFrame:(CGSize)size
  51. {
  52. CGFloat r = radius+(lineWidth/2);
  53. CGFloat originX = size.width - (2*r) - 5;
  54. CGFloat originY = (size.height - (2*r))/2;
  55. self.frame = CGRectMake(originX, originY, r*2, r*2);
  56. self.countLabel.frame = CGRectMake(0, 0, r*2, r*2);
  57. }
  58. - (void)drawRect:(CGRect)rect
  59. {
  60. UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius+(lineWidth/2), radius+(lineWidth/2))
  61. radius:radius
  62. startAngle:DEGREES_TO_RADIANS(START_DEGREE_OFFSET)
  63. endAngle:DEGREES_TO_RADIANS(currentAngle)
  64. clockwise:YES];
  65. [self.color setStroke];
  66. aPath.lineWidth = lineWidth;
  67. [aPath stroke];
  68. _countLabel.text = [NSString stringWithFormat:@"%d", (int)currentTime];
  69. }
  70. - (void)startTimerWithTimeLimit:(int)tl completed:(SCLActionBlock)completed
  71. {
  72. if (_reverse)
  73. {
  74. currentTime = tl;
  75. }
  76. timerLimit = tl;
  77. timer = [NSTimer scheduledTimerWithTimeInterval:TIMER_STEP target:self selector:@selector(updateTimerButton:) userInfo:nil repeats:YES];
  78. completedBlock = completed;
  79. _countLabel.textColor = _color;
  80. }
  81. - (void)cancelTimer
  82. {
  83. [timer invalidate];
  84. }
  85. - (void)stopTimer
  86. {
  87. [timer invalidate];
  88. if (completedBlock != nil) {
  89. completedBlock();
  90. }
  91. }
  92. - (void)updateTimerButton:(NSTimer *)timer
  93. {
  94. if (_reverse)
  95. {
  96. currentTime -= TIMER_STEP;
  97. currentAngle = (currentTime/timerLimit) * 360 + START_DEGREE_OFFSET;
  98. if(currentTime <= 0) {
  99. [self stopTimer];
  100. }
  101. }
  102. else {
  103. currentTime += TIMER_STEP;
  104. currentAngle = (currentTime/timerLimit) * 360 + START_DEGREE_OFFSET;
  105. if(currentAngle >= (360 + START_DEGREE_OFFSET)) {
  106. [self stopTimer];
  107. }
  108. }
  109. [self setNeedsDisplay];
  110. }
  111. @end