Files
peko-ios/YuMi/CustomUI/Adbvertise/XPAdvertiseView.m
2025-03-21 16:19:07 +08:00

287 lines
9.5 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// YMAdvertiseView.m
// YUMI
//
// Created by YUMI on 2022/10/31.
//
#import "XPAdvertiseView.h"
#import "AppDelegate.h"
//tool
#import <SVGA.h>
#import <sys/sysctl.h>
#import <sys/utsname.h>
#import "YUMIMacroUitls.h"
///Tool
#import "UIImage+Utils.h"
NSString *const adImageName = @"adImageName";
NSString *const adUrl = @"adUrl";
// 广告显示的时间
static int const showtime = 3;
@interface XPAdvertiseView() <SVGAPlayerDelegate>
@property(nonatomic, strong) SVGAImageView *svgaView;
@property (nonatomic, strong) UIImageView *adView;//广告图片
@property (nonatomic, strong) UIButton *countdownButton;//倒计时、跳过按钮
@property (nonatomic, strong) NSTimer *countTimer;
@property (nonatomic, assign) int count;
@property (nonatomic, strong) UIWindow *window;
@property(nonatomic, strong) NSMutableArray *imageLoaders;
@end
@implementation XPAdvertiseView
#pragma mark - Initialize Methods
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_adView = [[UIImageView alloc] initWithFrame:frame];
_adView.userInteractionEnabled = YES;
_adView.contentMode = UIViewContentModeScaleAspectFill;
_adView.clipsToBounds = YES;
[self addSubview:_adView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapAdViewAction)];
[_adView addGestureRecognizer:tap];
[self addSubview:self.countdownButton];
[self.countdownButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.trailing.mas_equalTo(-24);
make.width.mas_equalTo(60);
make.height.mas_equalTo(30);
make.top.mas_equalTo(40);
}];
// // 因为倒数跳秒问题,导致无法及时响应事件,经测试提案说无法接受此结果。所以就做成和安卓一样,去掉倒计时和跳过
// if ([self needCountDownBtn]) {
// [self addSubview:self.countdownButton];
// }
}
return self;
}
#pragma mark - Public Methods
- (void)show {
// 倒计时方法1GCD
[self gcdCoundownHander];
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
[self.window.rootViewController.view addSubview:self];
[self.window makeKeyAndVisible];
[keyWindow makeKeyWindow];
}
#pragma mark - Event Response
/// 点击闪屏操作
- (void)onTapAdViewAction {
[self dismissWithJumpHandle:YES];
}
/// 点击跳过按钮
- (void)onClickSkipButton:(UIButton *)sender {
[self dismissWithJumpHandle:NO];
}
#pragma mark - Privite Method
// GCD倒计时方法
- (void)gcdCoundownHander {
__block int timeout = showtime;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0); //每秒执行
dispatch_source_set_event_handler(_timer, ^{
if (timeout <= 0) { //倒计时结束,关闭
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissWithJumpHandle:NO];
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
if (self.countdownButton) {
[self.countdownButton setTitle:YMLocalizedString(@"XPAdvertiseView0") forState:UIControlStateNormal];
}
});
timeout--;
}
});
dispatch_resume(_timer);
}
// 移除广告页面
- (void)dismissWithJumpHandle:(BOOL)shouldJump {
if (self.countTimer) {
[self.countTimer invalidate];
self.countTimer = nil;
}
@kWeakify(self)
[UIView animateWithDuration:0.5f animations:^{
@kStrongify(self)
self.window.hidden = YES;
} completion:^(BOOL finished) {
@kStrongify(self)
[self removeFromSuperview];
self.window = nil;
!self.dismissHandler ?: self.dismissHandler(shouldJump);
}];
}
- (NSString *)deviceName {
// Gets a string with the device model
size_t size;
int nR = sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = (char *)malloc(size);
nR = sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);
return platform;
}
- (BOOL)needCountDownBtn {
NSString *platform = [self deviceName];
BOOL needBtn = YES;
if ([platform isEqualToString:@"iPhone6,1"] ||
[platform isEqualToString:@"iPhone6,2"] ||
[platform isEqualToString:@"iPhone7,1"] ||
[platform isEqualToString:@"iPhone7,2"] ||
[platform isEqualToString:@"iPhone8,1"] ||
[platform isEqualToString:@"iPhone8,2"] ||
[platform isEqualToString:@"iPhone8,4"]) {
needBtn = NO;
}
return needBtn;
}
#pragma mark - Setter
- (void)setFilePath:(NSString *)filePath {
_filePath = filePath;
_imageLoaders = @[].mutableCopy;
if (self.type == SplashInfoSkipTypeWeb_CP || self.type == SplashInfoSkipTypeWeb_Custom || self.type == SplashInfoSkipTypeWeb_WeekStar) {
_svgaView = [[SVGAImageView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
_svgaView.delegate = self;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapAdViewAction)];
[_svgaView addGestureRecognizer:tap];
// [self addSubview:_svgaView];
[self insertSubview:_svgaView belowSubview:self.countdownButton];
SVGAParser *p = [[SVGAParser alloc] init];
@kWeakify(self);
[p parseWithURL:[[NSURL alloc] initFileURLWithPath:filePath] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
@kStrongify(self);
if (videoItem) {
self.svgaView.autoPlay = YES;
self.svgaView.clearsAfterStop = YES;
self.svgaView.videoItem = videoItem;
if (self.fileModel) {
[self updateSvgaImage:self.fileModel.avatar key:@"avatar"];
[self updateSvgaImage:self.fileModel.picUrl key:@"gift"];
[self updateSvgaImage:self.fileModel.avatar key:@"avatar_1"];
[self updateSvgaImage:self.fileModel.loverAvatar key:@"avatar_2"];
[self updateSvgaText:[NSString stringWithFormat:@"ID: %@", self.fileModel.erbanNo] key:@"id"];
[self updateSvgaText:self.fileModel.giftName key:@"name"];
[self updateSvgaText:[NSString stringWithFormat:@"ID: %@", self.fileModel.erbanNo] key:@"id_1"];
[self updateSvgaText:[NSString stringWithFormat:@"ID: %@", self.fileModel.loverErbanNo] key:@"id_2"];
}
[self.svgaView startAnimation];
}
} failureBlock:^(NSError * _Nullable error) {
@kStrongify(self);
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
self.adView.image = [image cutImage:[UIScreen mainScreen].bounds.size];
}];
} else {
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
self.adView.image = [image cutImage:[UIScreen mainScreen].bounds.size];
}
}
- (void)updateSvgaImage:(NSString *)imagePath key:(NSString *)key {
if (self.svgaView && ![NSString isEmpty:imagePath] && ![NSString isEmpty:key]) {
UIImage *image = [UIImage imageWithColor:[UIColor colorWithWhite:0.9 alpha:0.9] size:CGSizeMake(100, 100)];
[self.svgaView setImage:image
forKey:key];
__block NetImageView *loader = [[NetImageView alloc] init];
@kWeakify(self);
@kWeakify(loader);
[loader loadImageWithUrl:imagePath
completion:^(UIImage * _Nullable image, NSURL * _Nonnull url) {
@kStrongify(self);
@kStrongify(loader);
[self.svgaView setImage:image
forKey:key];
[self.imageLoaders removeObject:loader];
}];
[self.imageLoaders addObject:loader];
}
}
- (void)updateSvgaText:(NSString *)content key:(NSString *)key {
if (self.svgaView && ![NSString isEmpty:content] && ![NSString isEmpty:key]) {
NSAttributedString *string = [[NSAttributedString alloc] initWithString:content
attributes:@{
NSFontAttributeName: kFontMedium(kGetScaleWidth(24)),
NSForegroundColorAttributeName: UIColorFromRGB(0xFDF565)
}];
[self.svgaView setAttributedText:string
forKey:key];
}
}
- (void)setAdImage:(UIImage *)adImage {
_adImage = adImage;
_adView.image = [adImage cutImage:[UIScreen mainScreen].bounds.size];
}
#pragma mark - SVGAPlayerDelegate
#pragma mark - Getter
- (UIWindow *)window {
if (_window == nil) {
_window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
_window.windowLevel = UIWindowLevelAlert;
_window.userInteractionEnabled = YES;
_window.rootViewController = [[UIViewController alloc] init];
}
return _window;
}
- (UIButton *)countdownButton {
if (_countdownButton == nil) {
_countdownButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_countdownButton addTarget:self action:@selector(onClickSkipButton:) forControlEvents:UIControlEventTouchUpInside];
[_countdownButton setTitle:YMLocalizedString(@"XPAdvertiseView1") forState:UIControlStateNormal];
_countdownButton.titleLabel.font = [UIFont systemFontOfSize:15];
[_countdownButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_countdownButton.backgroundColor = [UIColor colorWithRed:38 /255.0 green:38 /255.0 blue:38 /255.0 alpha:0.6];
_countdownButton.layer.cornerRadius = 4;
}
return _countdownButton;
}
@end