// // 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" #import "GiGaHelper.h" @interface GiGaStartAdView() @property (nonatomic,strong) UIButton *countBtn; @property (nonatomic,strong) NSTimer *countTimer; @property (nonatomic,assign) int count; @end static int const showTime = 5; @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 = GIGARGB(131, 10,10, 1); _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 = 80; CGFloat btnH = 26; _countBtn = [[UIButton alloc] initWithFrame:CGRectMake(KMainW - btnW - 24,Phone_STATUBAR_H + 10, btnW, btnH)]; [_countBtn addTarget:self action:@selector(removeAdView) forControlEvents:UIControlEventTouchUpInside]; // [_countBtn setTitle:[NSString stringWithFormat:@"跳过%d",showTime] forState:UIControlStateNormal]; [_countBtn setTitle:GIGALocalComm(@"masktime_adview_passadtitle") forState:UIControlStateNormal]; _countBtn.titleLabel.font = [UIFont fontWithName:GIGA_FONTBOLD size:14]; [_countBtn setTitleColor:GIGARGB(144, 25, 31, 1) forState:UIControlStateNormal]; _countBtn.backgroundColor = GIGARGB(235,234,234,1) ; _countBtn.layer.masksToBounds = YES; _countBtn.layer.cornerRadius = _countBtn.frame.size.height / 2 ; [self addSubview:_adView]; [self addSubview:_countBtn]; UIImageView *imageViewlogo = [[UIImageView alloc] init]; imageViewlogo.image = [UIImage imageNamed:@"bg_logo"]; UIImageView *imageView27Mask = [[UIImageView alloc] init]; imageView27Mask.image = [UIImage imageNamed:@"bg_name"]; imageView27Mask.contentMode = UIViewContentModeScaleAspectFit; UILabel *detailLabel = [[UILabel alloc] init]; detailLabel.attributedText = [GiGaHelper stringWithText:GIGALocalComm(@"masktime_adview_desc") textColor:[UIColor whiteColor] textFont:GIGA_TEXTFONTBOLD(14) leterSpace:2]; UIButton *moreinfoBtn = [UIButton buttonWithType:UIButtonTypeCustom]; // [moreinfoBtn setImage:[UIImage imageNamed:@"btn_more"] forState:UIControlStateNormal]; [moreinfoBtn setBackgroundImage:[UIImage imageNamed:@"btn_more"] forState:UIControlStateNormal]; [moreinfoBtn addTarget:self action:@selector(gotoAd:) forControlEvents:UIControlEventTouchUpInside]; NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"了解更多" attributes:@{NSFontAttributeName:[UIFont fontWithName:GIGA_FONTNAME size:18],NSForegroundColorAttributeName:[UIColor whiteColor]}]; [moreinfoBtn setAttributedTitle:title forState:UIControlStateNormal]; UILabel *year = [[UILabel alloc] init]; year.textColor = [UIColor whiteColor]; year.font = GIGA_TEXTFONTBOLD(10); year.text = @"2018"; [self addSubview:imageViewlogo]; [self addSubview:imageView27Mask]; [self addSubview:detailLabel]; [self addSubview:moreinfoBtn]; [self addSubview:year]; [imageViewlogo mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.mas_top).offset(131); make.height.and.width.mas_equalTo(95); make.centerX.mas_equalTo(self.mas_centerX); }]; [imageView27Mask mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.mas_top).offset(271); make.width.mas_equalTo(120); make.height.mas_equalTo(70); make.centerX.mas_equalTo(self.mas_centerX); }]; [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.mas_top).offset(362); make.width.mas_equalTo(180); make.height.mas_equalTo(21); make.centerX.mas_equalTo(self.mas_centerX); }]; [moreinfoBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.mas_top).offset(478); make.width.mas_equalTo(135); make.height.mas_equalTo(35); make.centerX.mas_equalTo(self.mas_centerX); }]; [year mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(self.mas_bottom).offset(-(10 + PhoneX_BottomMargin)); make.width.mas_equalTo(31); make.height.mas_equalTo(14); make.left.mas_equalTo(20); }]; moreinfoBtn.hidden = YES; } -(void)removeAdView{ [self.countTimer invalidate]; self.countTimer = nil; self.transform = CGAffineTransformMakeScale(1, 1); [UIView animateWithDuration:0.6f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.transform = CGAffineTransformMakeScale(1.4, 1.4); self.alpha = 0.f; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } -(void)gotoAd:(UIButton *)btn{ btn.userInteractionEnabled = NO; NC_POST_NAME_OBJECT(APP_PUSHTO_DETAILVIEW, nil); [NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(removeAdView) userInfo:nil repeats:NO]; //[self removeAdView]; } -(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