// // YMAdvertiseView.m // YUMI // // Created by YUMI on 2022/10/31. // #import "XPAdvertiseView.h" #import "AppDelegate.h" //tool #import #import #import "YUMIMacroUitls.h" ///Tool #import "UIImage+Utils.h" NSString *const adImageName = @"adImageName"; NSString *const adUrl = @"adUrl"; // 广告显示的时间 static int const showtime = 3; @interface XPAdvertiseView() @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 { // 倒计时方法1:GCD [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 { } - (void)updateSvgaImage:(NSString *)imagePath key:(NSString *)key { } - (void)updateSvgaText:(NSString *)content key:(NSString *)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; _countdownButton.contentEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 8); [_countdownButton sizeToFit]; } return _countdownButton; } @end