DemoBoxItem.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // DemoBoxItem.m
  3. // xh_textfield
  4. //
  5. // Created by 路 on 2019/10/24.
  6. // Copyright © 2019年 路. All rights reserved.
  7. //
  8. #import "DemoBoxItem.h"
  9. @implementation DemoBoxItem {
  10. NSTimer *_timer;
  11. UIView *_twinkle_line;
  12. }
  13. - (id)init {
  14. self = [super init];
  15. if (self) {
  16. self.layer.borderWidth = 2.f;
  17. self.layer.cornerRadius = 5.f;
  18. self.layer.masksToBounds = YES;
  19. _twinkle_line = [[UIView alloc] init];
  20. _twinkle_line.backgroundColor = [UIColor orangeColor];
  21. _twinkle_line.hidden = YES;
  22. [self addSubview:_twinkle_line];
  23. _label = [[UILabel alloc] init];
  24. _label.font = [UIFont systemFontOfSize:15];
  25. _label.textAlignment = NSTextAlignmentCenter;
  26. [self addSubview:_label];
  27. } return self;
  28. }
  29. - (void)layoutSubviews {
  30. [super layoutSubviews];
  31. _twinkle_line.bounds = CGRectMake(0, 0, 2.f, 15.f);
  32. _twinkle_line.center = CGPointMake(self.frame.size.width * .5, self.frame.size.height * .5);
  33. _label.frame = self.bounds;
  34. }
  35. - (void)update:(NSTimer *)sender {
  36. if (self.activated) {
  37. _twinkle_line.hidden = !_twinkle_line.hidden;
  38. }
  39. }
  40. - (void)setActivated:(BOOL)activated {
  41. [super setActivated:activated];
  42. if (activated) {
  43. self.layer.borderColor = [UIColor orangeColor].CGColor;
  44. _label.textColor = [UIColor orangeColor];
  45. if (!_timer) {
  46. _timer = [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(update:) userInfo:nil repeats:YES];
  47. [_timer fire];
  48. }
  49. } else {
  50. self.layer.borderColor = [UIColor grayColor].CGColor;
  51. _label.textColor = [UIColor grayColor];
  52. [self destroy];
  53. _twinkle_line.hidden = YES;
  54. }
  55. }
  56. - (void)setText:(NSString *)text; {
  57. [super setText:text];
  58. _label.text = text;
  59. }
  60. - (void)destroy; {
  61. [_timer invalidate];
  62. _timer = nil;
  63. }
  64. @end