MOGiftNumView.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // MOGiftNumView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2024/8/22.
  6. //
  7. #import "MOGiftNumView.h"
  8. #import "UIImageView+Gift.h"
  9. @interface MOGiftNumView ()
  10. @property (weak, nonatomic) IBOutlet UIImageView *giftImgView;
  11. @property (weak, nonatomic) IBOutlet UIStackView *theStackView;
  12. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *theStackViewWidth;
  13. @end
  14. @implementation MOGiftNumView
  15. + (instancetype)moGiftNumView{
  16. return [[[NSBundle mainBundle] loadNibNamed:@"MOGiftNumView" owner:self options:nil] firstObject];
  17. }
  18. - (void)awakeFromNib{
  19. [super awakeFromNib];
  20. }
  21. - (void)toShowNewNumber{
  22. if(self.theNum == 0){
  23. self.theNum = 1;
  24. }
  25. self.theStackViewWidth.constant = [MOGiftNumView getTheViewWidth:self.theNum];
  26. for (UIView *subview in self.theStackView.arrangedSubviews) {
  27. [self.theStackView removeArrangedSubview:subview];
  28. [subview removeFromSuperview];
  29. }
  30. NSArray *theImgViewArr = [self toGetTheNumImageViewArr];
  31. for (UIImageView *imgView in theImgViewArr) {
  32. [self.theStackView addArrangedSubview:imgView];
  33. }
  34. }
  35. - (void)setGiftInfo:(MOGiftInfo *)giftInfo{
  36. _giftInfo = giftInfo;
  37. [self.giftImgView mo_setGiftImageWith:giftInfo.giftPath];
  38. }
  39. + (CGFloat)getTheViewWidth:(NSInteger)theNum{
  40. NSString *numberString = [NSString stringWithFormat:@"%zd", theNum];
  41. NSUInteger length = [numberString length] + 1;
  42. // 15 * 15
  43. //13.0 是希望图片更紧凑一点
  44. //self.theStackView.spacing = -10
  45. CGFloat width = length * 9.0;
  46. return width;
  47. }
  48. - (NSArray *)toGetTheNumImageViewArr{
  49. NSString *numberString = [NSString stringWithFormat:@"%zd", self.theNum];
  50. NSUInteger length = [numberString length];
  51. NSMutableArray *tempArr = [NSMutableArray array];
  52. UIImage *numImg = [UIImage imageNamed:@"icon_voice_x"];
  53. UIImageView *numImgView = [[UIImageView alloc] initWithImage:numImg];
  54. numImgView.frame = CGRectMake(0.0, 0.0, 15.0, 15.0);
  55. numImgView.contentMode = UIViewContentModeScaleAspectFit;
  56. [tempArr addObject:numImgView];
  57. // 3. 遍历每一位数字
  58. for (NSUInteger i = 0; i < length; i++) {
  59. // 获取每一位数字
  60. unichar digitChar = [numberString characterAtIndex:i];
  61. // 将字符转换为整数值
  62. NSInteger digit = [[NSString stringWithFormat:@"%c", digitChar] integerValue];
  63. NSString *imgStr = [NSString stringWithFormat:@"icon_voice_%zd",digit];
  64. UIImage *numImg = [UIImage imageNamed:imgStr];
  65. UIImageView *numImgView = [[UIImageView alloc] initWithImage:numImg];
  66. numImgView.frame = CGRectMake(0.0, 0.0, 10.0, 10.0);
  67. numImgView.contentMode = UIViewContentModeScaleAspectFit;
  68. [tempArr addObject:numImgView];
  69. }
  70. return [tempArr copy];
  71. }
  72. @end