UGCKitBGMProgressView.m 3.7 KB

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