MHBeautySlider.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // MHBeautySlider.m
  3. #import "MHBeautySlider.h"
  4. @interface MHBeautySlider ()
  5. /*! @brief slider的thumbView */
  6. @property (nonatomic, strong) UIView *thumbView;
  7. /*! @brief 显示value的label */
  8. @property (nonatomic, strong) UILabel *valueLabel;
  9. @end
  10. @implementation MHBeautySlider
  11. - (instancetype)initWithFrame:(CGRect)frame {
  12. if (self = [super initWithFrame:frame]) {
  13. [self addTarget:self action:@selector(sliderTouchDown:) forControlEvents:UIControlEventTouchDown];
  14. [self addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
  15. [self addTarget:self action:@selector(sliderTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
  16. [self.thumbView addSubview:self.valueLabel];
  17. }
  18. return self;
  19. }
  20. #pragma mark - Overwrite functions
  21. - (CGRect)trackRectForBounds:(CGRect)bounds {
  22. /*! @brief 锁屏后_thumbView消失,所以在这里重新赋值 */
  23. if (@available(iOS 14, *)) {
  24. if (self.subviews.count > 0) {
  25. if (self.subviews[0].subviews.count > 2) {
  26. _thumbView = self.subviews[0].subviews[2];
  27. [_thumbView addSubview:self.valueLabel];
  28. }
  29. }
  30. }
  31. /*! @brief 重写方法-返回进度条的bounds-修改进度条的高度 */
  32. bounds = [super trackRectForBounds:bounds];
  33. return CGRectMake(bounds.origin.x, bounds.origin.y + (bounds.size.height - 3.0) / 2, bounds.size.width, 3.0);
  34. }
  35. - (void)setValue:(float)value animated:(BOOL)animated {
  36. [super setValue:value animated:animated];
  37. [self sliderValueChanged:self];
  38. }
  39. - (void)setValue:(float)value {
  40. [super setValue:value];
  41. [self sliderValueChanged:self];
  42. }
  43. #pragma mark - Setter functions
  44. - (void)setSliderValue:(NSString *)sliderValue {
  45. // self.backgroundColor = UIColor.yellowColor;
  46. if (![_sliderValue isEqualToString:sliderValue]) {
  47. _sliderValue = sliderValue;
  48. self.valueLabel.text = sliderValue;
  49. [self.valueLabel sizeToFit];
  50. self.valueLabel.center = CGPointMake(self.thumbView.bounds.size.width / 2, -self.valueLabel.bounds.size.height / 2);
  51. if (!self.valueLabel.superview) {
  52. [self.thumbView addSubview:self.valueLabel];
  53. }
  54. }
  55. }
  56. #pragma mark - Getter functions
  57. - (UIView *)thumbView {
  58. if (!_thumbView && self.subviews.count > 2) {
  59. _thumbView = self.subviews[2];
  60. }
  61. if (@available(iOS 14, *)) {
  62. if (!_thumbView && self.subviews[0].subviews.count > 2) {
  63. _thumbView = self.subviews[0].subviews[2];
  64. }
  65. }
  66. return _thumbView;
  67. }
  68. - (UILabel *)valueLabel {
  69. if (!_valueLabel) {
  70. _valueLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  71. _valueLabel.textColor = [UIColor whiteColor];
  72. _valueLabel.font = [UIFont systemFontOfSize:14.0];
  73. _valueLabel.textAlignment = NSTextAlignmentCenter;
  74. }
  75. return _valueLabel;
  76. }
  77. #pragma mark - Action functions
  78. - (void)sliderTouchDown:(MHBeautySlider *)sender {
  79. if (_touchDown) {
  80. _touchDown(sender);
  81. }
  82. }
  83. - (void)sliderValueChanged:(MHBeautySlider *)sender {
  84. if (_valueChanged) {
  85. _valueChanged(sender);
  86. } else {
  87. [sender setSliderValue: [NSString stringWithFormat:@"%ld", (long)sender.value]];
  88. }
  89. }
  90. - (void)sliderTouchUpInside:(MHBeautySlider *)sender {
  91. if (_touchUpInside) {
  92. _touchUpInside(sender);
  93. }
  94. }
  95. #pragma mark -
  96. - (void)dealloc {
  97. NSLog(@"%s", __FUNCTION__);
  98. }
  99. @end