From 21d3023a9b7b6aff68c1170e345951396b1c6cfd Mon Sep 17 00:00:00 2001 From: 单军华 Date: Tue, 31 Jul 2018 13:35:21 +0800 Subject: [PATCH] no message --- screendisplay/screendisplay/Classes/Helpers/广告页/AdvertiseView.m | 125 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 125 insertions(+), 0 deletions(-) diff --git "a/screendisplay/screendisplay/Classes/Helpers/\345\271\277\345\221\212\351\241\265/AdvertiseView.m" "b/screendisplay/screendisplay/Classes/Helpers/\345\271\277\345\221\212\351\241\265/AdvertiseView.m" new file mode 100755 index 0000000..0afa69b --- /dev/null +++ "b/screendisplay/screendisplay/Classes/Helpers/\345\271\277\345\221\212\351\241\265/AdvertiseView.m" @@ -0,0 +1,125 @@ +// +// AdvertiseView.m +// MobileProject +// +// Created by wujunyang on 16/6/14. +// Copyright �� 2016��� wujunyang. All rights reserved. +// + +#import "AdvertiseView.h" + +NSString *const NotificationContants_Advertise_Key = @"NotificationContants_Advertise_Key"; + +@interface AdvertiseView() + +@property (nonatomic, strong) YYAnimatedImageView *adView; +@property (nonatomic, strong) UIButton *countBtn; +@property (nonatomic, assign) NSUInteger count; + +@property (nonatomic, strong) dispatch_source_t gcdTimer; +@end + +//��������������������� +static const NSUInteger showtime = 5; + +@implementation AdvertiseView + + +- (instancetype)initWithFrame:(CGRect)frame +{ + if (self = [super initWithFrame:frame]) { + + // 1.������������ + _adView = [[YYAnimatedImageView alloc] initWithFrame:frame]; + _adView.userInteractionEnabled = YES; + _adView.contentMode = UIViewContentModeScaleAspectFill; + _adView.clipsToBounds = YES; + UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToAd)]; + [_adView addGestureRecognizer:tap]; + + // 2.������������ + CGFloat btnW = 60; + CGFloat btnH = 30; + _countBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - btnW - 24, [UIApplication sharedApplication].statusBarFrame.size.height + btnH, btnW, btnH)]; + [_countBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside]; + [_countBtn setTitle:[NSString stringWithFormat:@"������%zd", showtime] forState:UIControlStateNormal]; + _countBtn.titleLabel.font = [UIFont systemFontOfSize:15]; + [_countBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; + _countBtn.backgroundColor = [UIColor colorWithRed:38 /255.0 green:38 /255.0 blue:38 /255.0 alpha:0.6]; + _countBtn.layer.cornerRadius = 4; + + [self addSubview:_adView]; + [self addSubview:_countBtn]; + + } + return self; +} + +- (void)setFilePath:(NSString *)filePath +{ + _filePath = filePath; + _adView.image = [YYImage imageWithContentsOfFile:filePath]; +} + + +- (void)pushToAd{ + + [self dismiss]; + [[NSNotificationCenter defaultCenter] postNotificationName:NotificationContants_Advertise_Key object:nil userInfo:nil]; +} + + +- (void)show +{ + // ���������������1���GCD + [self startCoundown]; + + UIWindow *window = [UIApplication sharedApplication].keyWindow; + [window addSubview:self]; +} + + +// GCD��������� +- (void)startCoundown +{ + __block int timeout = showtime + 1; //��������������� + 1 + TWWeak(self); + // GCD ��������� + self.gcdTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue()); + dispatch_source_set_timer(self.gcdTimer, dispatch_time(DISPATCH_TIME_NOW, 0), 1 * NSEC_PER_SEC, 0); //������������ + dispatch_source_set_event_handler(self.gcdTimer, ^{ + if(timeout <= 0){ //������������������������ + [weakself dismiss]; + }else { + dispatch_async(dispatch_get_main_queue(), ^{ + [_countBtn setTitle:[NSString stringWithFormat:@"������%d",timeout] forState:UIControlStateNormal]; + }); + timeout--; + } + }); + dispatch_resume(self.gcdTimer); +} + +// ������������������ +- (void)dismiss +{ + if (_gcdTimer) { + dispatch_cancel(_gcdTimer); + } + _gcdTimer = nil; + dispatch_async(dispatch_get_main_queue(), ^{ + [UIView animateWithDuration:0.3f animations:^{ + self.alpha = 0.f; + } completion:^(BOOL finished) { + [self removeFromSuperview]; + }]; + + }); +} + +- (void)dealloc +{ + _gcdTimer = nil; +} + +@end -- Gitblit v1.8.0