Files
peko-ios/YuMi/Modules/YMRoom/View/AnimationView/XPRoomGiftCompoundView.m
2024-04-11 17:05:27 +08:00

114 lines
3.8 KiB
Objective-C

//
// YMRoomGiftCompoundView.m
// YUMI
//
// Created by YUMI on 2022/8/5.
//
#import "XPRoomGiftCompoundView.h"
///Third
#import <Masonry/Masonry.h>
#import <SVGA.h>
#import "YUMIMacroUitls.h"
#import "ThemeColor+Room.h"
#import "XPGiftCompoundModel.h"
#import "NSObject+MJExtension.h"
@interface XPRoomGiftCompoundView ()
///动画管理类
@property (strong, nonatomic) SVGAParser *parser;
///礼物合成特效
@property (nonatomic,strong) SVGAImageView *compoundGiftView;
///显示文本内容
@property (nonatomic,strong) UILabel *titleLabel;
@end
@implementation XPRoomGiftCompoundView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
[self addSubview:self.compoundGiftView];
[self addSubview:self.titleLabel];
}
- (void)initSubViewConstraints {
[self.compoundGiftView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.top.mas_equalTo(self);
make.height.mas_equalTo(45);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.compoundGiftView);
}];
}
- (NSAttributedString *)createAttribute:(NSString * )text color:(UIColor *)color fontSize:(CGFloat)fonSize {
NSDictionary * attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:fonSize], NSForegroundColorAttributeName:color};
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:text attributes:attribute];
return attr;
}
#pragma mark - Getters And Setters
- (void)setCompoundGiftInfo:(NSDictionary *)compoundGiftInfo {
if (compoundGiftInfo) {
XPGiftCompoundModel * giftInfo = [XPGiftCompoundModel modelWithDictionary:compoundGiftInfo];
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init];
CGFloat fontSize = 22;
NSString *nickStr = giftInfo.nick;
if (nickStr.length > 6) {
nickStr = [NSString stringWithFormat:@"%@…", [giftInfo.nick substringToIndex:6]];
}
[attribute appendAttributedString:[self createAttribute:YMLocalizedString(@"XPRoomGiftCompoundView0") color:[UIColor whiteColor] fontSize:fontSize]];
[attribute appendAttributedString:[self createAttribute:nickStr color:[DJDKMIMOMColor messageNickColor] fontSize:fontSize]];
[attribute appendAttributedString:[self createAttribute:giftInfo.msg color:[UIColor whiteColor] fontSize:fontSize]];
[attribute appendAttributedString:[self createAttribute:giftInfo.giftName color:[DJDKMIMOMColor messageNickColor] fontSize:fontSize]];
@kWeakify(self);
NSString * anatomiser1Name = [NSString stringWithFormat:@"%@/compound_gift_banner.svga", API_Image_URL];
[self.parser parseWithURL:[NSURL URLWithString:anatomiser1Name] completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
@kStrongify(self);
self.compoundGiftView.loops = 1;
self.compoundGiftView.clearsAfterStop = NO;
self.compoundGiftView.videoItem = videoItem;
[self.compoundGiftView setAttributedText:attribute forKey:@"noble_text_tx"];
[self.compoundGiftView startAnimation];
} failureBlock:^(NSError * _Nonnull error) {
}];
}
}
- (SVGAImageView *)compoundGiftView {
if (!_compoundGiftView) {
_compoundGiftView = [[SVGAImageView alloc]init];
_compoundGiftView.backgroundColor = [UIColor clearColor];
_compoundGiftView.userInteractionEnabled = NO;
}
return _compoundGiftView;
}
- (SVGAParser *)parser {
if (!_parser) {
_parser = [[SVGAParser alloc]init];
}
return _parser;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
}
return _titleLabel;
}
@end