152 lines
4.5 KiB
Objective-C
152 lines
4.5 KiB
Objective-C
//
|
|
// GiGaStartAdView.m
|
|
// GIGA
|
|
//
|
|
// Created by lianxiang on 2018/8/22.
|
|
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
|
//
|
|
|
|
#import "GiGaStartAdView.h"
|
|
#import "GiGaFileNanager.h"
|
|
#import "UIImageView+WebCache.h"
|
|
|
|
@interface GiGaStartAdView()
|
|
|
|
@property (nonatomic,strong) UIButton *countBtn;
|
|
@property (nonatomic,strong) NSTimer *countTimer;
|
|
@property (nonatomic,assign) int count;
|
|
|
|
|
|
@end
|
|
static int const showTime = 12;
|
|
|
|
@implementation GiGaStartAdView
|
|
|
|
- (NSTimer *)countTimer{
|
|
|
|
if (!_countTimer) {
|
|
|
|
_countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
|
|
|
|
}
|
|
return _countTimer;
|
|
}
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame{
|
|
if (self = [super initWithFrame:frame]) {
|
|
[self initUIWithFrame:frame];
|
|
}
|
|
return self;
|
|
|
|
}
|
|
-(void)initUIWithFrame:(CGRect)frame{
|
|
|
|
self.backgroundColor = [UIColor whiteColor];
|
|
_adView = [[UIImageView alloc] initWithFrame:frame];
|
|
_adView.userInteractionEnabled = YES;
|
|
_adView.contentMode = UIViewContentModeScaleAspectFill;
|
|
_adView.clipsToBounds = YES;
|
|
//广告点击手势
|
|
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoAd)];
|
|
[_adView addGestureRecognizer:tap];
|
|
CGFloat btnW = 60;
|
|
CGFloat btnH = 30;
|
|
_countBtn = [[UIButton alloc] initWithFrame:CGRectMake(KMainW - btnW - 24, btnH, btnW, btnH)];
|
|
[_countBtn addTarget:self action:@selector(removeAdView) forControlEvents:UIControlEventTouchUpInside];
|
|
[_countBtn setTitle:[NSString stringWithFormat:@"跳过%d",showTime] forState:UIControlStateNormal];
|
|
_countBtn.titleLabel.font = [UIFont systemFontOfSize:15];
|
|
[_countBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
|
_countBtn.backgroundColor = GIGARGB(38, 38, 38, 0.6) ;
|
|
_countBtn.layer.masksToBounds = YES;
|
|
_countBtn.layer.cornerRadius = 4;
|
|
[self addSubview:_adView];
|
|
[self addSubview:_countBtn];
|
|
|
|
}
|
|
|
|
-(void)removeAdView{
|
|
|
|
[self.countTimer invalidate];
|
|
self.countTimer = nil;
|
|
[UIView animateWithDuration:0.3f animations:^{
|
|
self.alpha = 0.f;
|
|
} completion:^(BOOL finished) {
|
|
[self removeFromSuperview];
|
|
}];
|
|
}
|
|
|
|
-(void)gotoAd{
|
|
|
|
[self removeAdView];
|
|
NC_POST_NAME_OBJECT(APP_PUSHTO_DETAILVIEW, nil);
|
|
|
|
}
|
|
|
|
-(void)countDown{
|
|
|
|
_count --;
|
|
[_countBtn setTitle:[NSString stringWithFormat:@"跳过%d",_count] forState:UIControlStateNormal];
|
|
if (_count == 0) {
|
|
[self removeAdView];
|
|
}
|
|
}
|
|
|
|
|
|
-(void)show{
|
|
|
|
[self loadImage];
|
|
UIWindow *window = [UIApplication sharedApplication].keyWindow;
|
|
[window addSubview:self];
|
|
//[self startTimer];
|
|
//GCD 备用
|
|
[self gcdStartCount];
|
|
|
|
}
|
|
|
|
-(void)loadImage{
|
|
|
|
NSArray *imageArray = @[ @"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a4b3d7085dee3d6d2293d48b252b5910/0e2442a7d933c89524cd5cd4d51373f0830200ea.jpg", @"https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a41eb338dd33c895a62bcb3bb72e47c2/5fdf8db1cb134954a2192ccb524e9258d1094a1e.jpg" ];
|
|
NSString *imageUrl = imageArray[arc4random() % imageArray.count]; // 获取图片名:43-130P5122Z60-50.jpg
|
|
|
|
[_adView sd_setImageWithURL:[NSURL URLWithString:imageUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
|
|
NSLog(@"%@",error);
|
|
|
|
}];
|
|
|
|
|
|
}
|
|
|
|
-(void)startTimer{
|
|
_count = showTime;
|
|
[[NSRunLoop mainRunLoop] addTimer:self.countTimer forMode:NSRunLoopCommonModes];
|
|
|
|
}
|
|
-(void)gcdStartCount{
|
|
|
|
weakify(self);
|
|
__block int timeout = showTime + 1; //倒计时时间 + 1
|
|
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_walltime(NULL, 0),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(), ^{
|
|
|
|
[weakSelf removeAdView];
|
|
|
|
});
|
|
|
|
}else{ dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[weakSelf.countBtn setTitle:[NSString stringWithFormat:@"跳过%d",timeout] forState:UIControlStateNormal];
|
|
|
|
});
|
|
timeout--;
|
|
|
|
} });
|
|
dispatch_resume(_timer);
|
|
}
|
|
|
|
|
|
|
|
@end
|