186 lines
7.2 KiB
Objective-C
186 lines
7.2 KiB
Objective-C
//
|
|
// PIUniversalBannerView.m
|
|
// YuMi
|
|
//
|
|
// Created by duoban on 2024/3/19.
|
|
//
|
|
#import <Masonry/Masonry.h>
|
|
#import "YUMIMacroUitls.h"
|
|
#import "DJDKMIMOMColor.h"
|
|
#import "PIUniversalBannerView.h"
|
|
#import <SVGA.h>
|
|
#import "DJDKMIMOMColor.h"
|
|
#import "YuMi-swift.h"
|
|
@interface PIUniversalBannerView()
|
|
@property (strong, nonatomic) SVGAParser *parser;
|
|
@property (nonatomic,strong) SVGAImageView *svgaView;
|
|
@property (nonatomic,strong) UIImageView *bgImageView;
|
|
@property(nonatomic, strong) NetImageView *imageView;
|
|
@property (nonatomic,strong) UILabel *titleView;
|
|
@property (nonatomic,strong) UIButton *clickBtn;
|
|
@property(nonatomic,strong) SVGAVideoEntity *videoItem;
|
|
|
|
@end
|
|
@implementation PIUniversalBannerView
|
|
|
|
|
|
-(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.bgImageView];
|
|
[self addSubview:self.svgaView];
|
|
[self addSubview:self.titleView];
|
|
[self addSubview:self.clickBtn];
|
|
|
|
}
|
|
- (void)initSubViewConstraints {
|
|
[self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.equalTo(self);
|
|
}];
|
|
[self.svgaView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.equalTo(self);
|
|
}];
|
|
[self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.top.centerX.bottom.equalTo(self);
|
|
make.width.height.mas_equalTo(kGetScaleWidth(255));
|
|
}];
|
|
|
|
[self.clickBtn mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.equalTo(self);
|
|
}];
|
|
|
|
}
|
|
-(void)setModel:(PIUniversalBannerModel *)model{
|
|
_model = model;
|
|
NSDictionary *textDic = model.template;
|
|
if(textDic.allKeys.count == 0)return ;
|
|
NSString *key = [NSBundle uploadLanguageText];
|
|
NSString *title = textDic[key] == nil ? textDic[textDic.allKeys.firstObject] : textDic[key];
|
|
if(title.length == 0)return;
|
|
[title stringByReplacingOccurrencesOfString:@"\n" withString:@""];
|
|
if (_model.fontSize <= 0){
|
|
_model.fontSize = 12;
|
|
}
|
|
CGFloat font = _model.fontSize;
|
|
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:kFontRegular(font), NSForegroundColorAttributeName:[DJDKMIMOMColor colorWithHexString:_model.textColor]}];
|
|
|
|
dispatch_group_t g = dispatch_group_create();
|
|
@kWeakify(self);
|
|
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
|
for (PIUniversalBannerItemModel *model in self.model.contents) {
|
|
if([model.type isEqualToString:@"TEXT"]){
|
|
dispatch_group_enter(g);
|
|
NSDictionary *subTextDic = model.text;
|
|
if(subTextDic.allKeys.count > 0){
|
|
NSString *subText = subTextDic[key] == nil ? subTextDic[subTextDic.allKeys.firstObject] : subTextDic[key];
|
|
NSAttributedString *attText = [[NSAttributedString alloc]initWithString:subText attributes:@{NSForegroundColorAttributeName:[DJDKMIMOMColor colorWithHexString:model.textColor],NSFontAttributeName:[UIFont systemFontOfSize:font]}];
|
|
if ([attribute.string containsString:[NSString stringWithFormat:@"{%@}",model.key]]){
|
|
[attribute replaceCharactersInRange:[attribute.string rangeOfString:[NSString stringWithFormat:@"{%@}",model.key]] withAttributedString:attText];
|
|
}
|
|
}
|
|
dispatch_group_leave(g);
|
|
}else if ([model.type isEqualToString:@"IMAGE"]) {
|
|
if (![NSString isEmpty:model.key] ||
|
|
![NSString isEmpty:model.image]) {
|
|
dispatch_group_enter(g);
|
|
[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:model.image] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
if (error == nil && data != nil) {
|
|
UIImage *image = [UIImage imageWithData:data];
|
|
if (image != nil) {
|
|
if ([attribute.string containsString:[NSString stringWithFormat:@"{%@}",model.key]]){
|
|
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
|
|
|
|
attachment.image = [image setCornerWithRadius:model.height/2 andSize:CGSizeMake(model.width, model.height)];
|
|
attachment.bounds = CGRectMake(0, -model.height/3, model.width, model.height);
|
|
NSAttributedString *attImage = [NSAttributedString attributedStringWithAttachment:attachment];
|
|
[attribute replaceCharactersInRange:[attribute.string rangeOfString:[NSString stringWithFormat:@"{%@}",model.key]] withAttributedString:attImage];
|
|
}
|
|
}
|
|
}
|
|
dispatch_group_leave(g);
|
|
}] resume];
|
|
}
|
|
}
|
|
}
|
|
|
|
dispatch_group_notify(g, dispatch_get_main_queue(), ^{
|
|
@kStrongify(self);
|
|
if(self.isSvga == YES){
|
|
self.bgImageView.hidden = YES;
|
|
self.svgaView.hidden = NO;
|
|
self.titleView.hidden = NO;
|
|
self.svgaView.loops = 1;
|
|
self.titleView.attributedText = attribute;
|
|
self.svgaView.clearsAfterStop = NO;
|
|
self.svgaView.videoItem = self.model.videoItem;
|
|
[self.svgaView startAnimation];
|
|
}else{
|
|
self.bgImageView.hidden = NO;
|
|
self.bgImageView.image = self.model.image;
|
|
self.titleView.hidden = NO;
|
|
self.svgaView.hidden = YES;
|
|
self.titleView.attributedText = attribute;
|
|
}
|
|
|
|
if (self.allowToPlay) {
|
|
self.allowToPlay();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
-(void)clickAction{
|
|
if(self.delegate &&
|
|
[self.delegate respondsToSelector:@selector(pIUniversalBannerView:didClick:)]){
|
|
[self.delegate pIUniversalBannerView:self didClick:self.model];
|
|
}
|
|
}
|
|
#pragma mark -懒加载
|
|
- (UIImageView *)bgImageView{
|
|
if (!_bgImageView){
|
|
_bgImageView = [UIImageView new];
|
|
_bgImageView.userInteractionEnabled = YES;
|
|
}
|
|
return _bgImageView;
|
|
}
|
|
-(UILabel *)titleView{
|
|
if (!_titleView){
|
|
_titleView = [UILabel new];
|
|
_titleView.textAlignment = NSTextAlignmentCenter;
|
|
_titleView.numberOfLines = 0;
|
|
}
|
|
return _titleView;
|
|
}
|
|
- (SVGAParser *)parser {
|
|
if (!_parser) {
|
|
_parser = [[SVGAParser alloc]init];
|
|
}
|
|
return _parser;
|
|
}
|
|
- (SVGAImageView *)svgaView {
|
|
if (!_svgaView) {
|
|
_svgaView= [[SVGAImageView alloc]init];
|
|
_svgaView.backgroundColor = [UIColor clearColor];
|
|
_svgaView.userInteractionEnabled = YES;
|
|
}
|
|
return _svgaView;
|
|
}
|
|
-(UIButton *)clickBtn{
|
|
if (!_clickBtn){
|
|
_clickBtn = [UIButton new];
|
|
[_clickBtn addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
}
|
|
return _clickBtn;
|
|
}
|
|
|
|
@end
|