MOSuperNumView.m 2.1 KB

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