Files
yinmeng-ios/xplan-ios/Base/UI/Adbvertise/XPAdvertiseView.m

206 lines
5.5 KiB
Mathematica
Raw Normal View History

2022-10-31 12:15:01 +08:00
//
// XPAdvertiseView.m
// xplan-ios
//
// Created by on 2022/10/31.
//
#import "XPAdvertiseView.h"
#import "AppDelegate.h"
//tool
#import <sys/sysctl.h>
#import <sys/utsname.h>
#import "XPMacro.h"
///Tool
#import "UIImage+Utils.h"
NSString *const adImageName = @"adImageName";
2023-12-12 11:53:14 +08:00
2022-10-31 12:15:01 +08:00
// 广
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;
@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];
2022-10-31 15:40:12 +08:00
[self addSubview:self.countdownButton];
// //
// if ([self needCountDownBtn]) {
// [self addSubview:self.countdownButton];
// }
2022-10-31 12:15:01 +08:00
}
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:@"跳过" 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;
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
_adView.image = [image cutImage:[UIScreen mainScreen].bounds.size];
}
- (void)setAdImage:(UIImage *)adImage {
_adImage = adImage;
_adView.image = [adImage cutImage:[UIScreen mainScreen].bounds.size];
}
#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) {
CGFloat btnW = 60;
CGFloat btnH = 30;
_countdownButton = [UIButton buttonWithType:UIButtonTypeCustom];
_countdownButton.frame = CGRectMake(KScreenWidth - btnW - 24, btnH + 10, btnW, btnH);
[_countdownButton addTarget:self action:@selector(onClickSkipButton:) forControlEvents:UIControlEventTouchUpInside];
[_countdownButton setTitle:@"跳过" 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