TCBGMProgressView.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // TCBGMProgressView.m
  3. // TXXiaoShiPinDemo
  4. //
  5. // Created by shengcui on 2018/7/26.
  6. // Copyright © 2018年 tencent. All rights reserved.
  7. //
  8. #import "TCBGMProgressView.h"
  9. @implementation TCBGMProgressView
  10. {
  11. UIImage *_bgImage;
  12. UIView *_maskView;
  13. NSLayoutConstraint *_progressConstraint;
  14. }
  15. - (instancetype)initWithFrame:(CGRect)frame
  16. {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. [self setup];
  20. }
  21. return self;
  22. }
  23. - (void)awakeFromNib
  24. {
  25. [super awakeFromNib];
  26. [self setup];
  27. }
  28. - (void)setup {
  29. self.contentMode = UIViewContentModeRedraw;
  30. self.clipsToBounds = YES;
  31. _bgImage = [UIImage imageNamed:@"music_select_normal"];
  32. _label = [[UILabel alloc] initWithFrame:self.bounds];
  33. _label.alpha = 0.5;
  34. self.opaque = YES;
  35. _label.font = [UIFont systemFontOfSize:15];
  36. _label.contentMode = UIViewContentModeCenter;
  37. _label.backgroundColor = [UIColor clearColor];
  38. _label.autoresizesSubviews = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  39. _label.translatesAutoresizingMaskIntoConstraints = NO;
  40. UIImageView *imageView = [[UIImageView alloc] initWithImage:_bgImage];
  41. imageView.frame = self.bounds;
  42. imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  43. [self addSubview:imageView];
  44. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[imageView]-0-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:NSDictionaryOfVariableBindings(imageView)]];
  45. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[imageView]-0-|" options:NSLayoutFormatAlignAllTop metrics:nil views:NSDictionaryOfVariableBindings(imageView)]];
  46. _maskView = [[UIView alloc] initWithFrame:self.bounds];
  47. _maskView.backgroundColor = self.progressBackgroundColor;
  48. _maskView.translatesAutoresizingMaskIntoConstraints = NO;
  49. [self addSubview:_maskView];
  50. _progressConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_maskView attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
  51. [self addConstraint:_progressConstraint];
  52. [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_maskView attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
  53. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_maskView]-0-|" options:NSLayoutFormatAlignAllTop metrics:nil views:NSDictionaryOfVariableBindings(_maskView)]];
  54. [self addSubview:_label];
  55. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_label]-5-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:NSDictionaryOfVariableBindings(_label)]];
  56. [self addConstraint:[NSLayoutConstraint constraintWithItem:_label
  57. attribute:NSLayoutAttributeCenterY
  58. relatedBy:NSLayoutRelationEqual
  59. toItem:self
  60. attribute:NSLayoutAttributeCenterY
  61. multiplier:1
  62. constant:0]];
  63. }
  64. - (void)setProgressBackgroundColor:(UIColor *)progressBackgroundColor
  65. {
  66. _progressBackgroundColor = progressBackgroundColor;
  67. _maskView.backgroundColor = progressBackgroundColor;
  68. }
  69. - (void)setProgress:(float)progress
  70. {
  71. if (_progress == progress) return;
  72. _progress = progress;
  73. _progressConstraint.constant = - progress * self.bounds.size.width;
  74. [self layoutIfNeeded];
  75. }
  76. - (void)layoutSubviews
  77. {
  78. [super layoutSubviews];
  79. self.layer.cornerRadius = self.bounds.size.height / 2;
  80. }
  81. @end