// // MOGiftNumView.m // MiMoLive // // Created by SuperC on 2024/8/22. // #import "MOGiftNumView.h" #import "UIImageView+Gift.h" @interface MOGiftNumView () @property (weak, nonatomic) IBOutlet UIImageView *giftImgView; @property (weak, nonatomic) IBOutlet UIStackView *theStackView; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *theStackViewWidth; @end @implementation MOGiftNumView + (instancetype)moGiftNumView{ return [[[NSBundle mainBundle] loadNibNamed:@"MOGiftNumView" owner:self options:nil] firstObject]; } - (void)awakeFromNib{ [super awakeFromNib]; } - (void)toShowNewNumber{ if(self.theNum == 0){ self.theNum = 1; } self.theStackViewWidth.constant = [MOGiftNumView getTheViewWidth:self.theNum]; for (UIView *subview in self.theStackView.arrangedSubviews) { [self.theStackView removeArrangedSubview:subview]; [subview removeFromSuperview]; } NSArray *theImgViewArr = [self toGetTheNumImageViewArr]; for (UIImageView *imgView in theImgViewArr) { [self.theStackView addArrangedSubview:imgView]; } } - (void)setGiftInfo:(MOGiftInfo *)giftInfo{ _giftInfo = giftInfo; [self.giftImgView mo_setGiftImageWith:giftInfo.giftPath]; } + (CGFloat)getTheViewWidth:(NSInteger)theNum{ NSString *numberString = [NSString stringWithFormat:@"%zd", theNum]; NSUInteger length = [numberString length] + 1; // 15 * 15 //13.0 是希望图片更紧凑一点 //self.theStackView.spacing = -10 CGFloat width = length * 9.0; return width; } - (NSArray *)toGetTheNumImageViewArr{ NSString *numberString = [NSString stringWithFormat:@"%zd", self.theNum]; NSUInteger length = [numberString length]; NSMutableArray *tempArr = [NSMutableArray array]; UIImage *numImg = [UIImage imageNamed:@"icon_voice_x"]; UIImageView *numImgView = [[UIImageView alloc] initWithImage:numImg]; numImgView.frame = CGRectMake(0.0, 0.0, 15.0, 15.0); numImgView.contentMode = UIViewContentModeScaleAspectFit; [tempArr addObject:numImgView]; // 3. 遍历每一位数字 for (NSUInteger i = 0; i < length; i++) { // 获取每一位数字 unichar digitChar = [numberString characterAtIndex:i]; // 将字符转换为整数值 NSInteger digit = [[NSString stringWithFormat:@"%c", digitChar] integerValue]; NSString *imgStr = [NSString stringWithFormat:@"icon_voice_%zd",digit]; UIImage *numImg = [UIImage imageNamed:imgStr]; UIImageView *numImgView = [[UIImageView alloc] initWithImage:numImg]; numImgView.frame = CGRectMake(0.0, 0.0, 10.0, 10.0); numImgView.contentMode = UIViewContentModeScaleAspectFit; [tempArr addObject:numImgView]; } return [tempArr copy]; } @end