网上很多的实现方法很多都是显示第一次的缓存的图片,这样就造成后台更新广告图片App不能实时展示的问题。
我的具体实现思路是: 1.启动时先获取启动页的图片全屏展示。 2.设计一个等待时间,如果超过等待时间还没拿到图片就把获取的启动页去掉,否则就显示广告页。
具体代码的实现:
@interface SSLaunchAdView : UIView
@property (nonatomic, strong) UIImageView *imageView;
///图片的下载链接
@property (nonatomic, strong) NSString *imageUrl;
///广告的显示时间
@property (nonatomic, assign) NSInteger AdTime;
///要等待的时间
@property (nonatomic, assign) NSInteger waitTime;
@property (nonatomic, copy) void(^clickImageView)(SSLaunchAdView *view);
- (id)initWithFrame:(CGRect)frame
AdTime:(NSInteger)AdTime
waitTime:(NSInteger)waitTime
onView:(UIView *)onView;
///点击广告响应
- (void)clickImageView:(void(^)(SSLaunchAdView *view))block;
///删除广告页
- (void)hideView;
@end
#import "SSLaunchAdView.h"
#import "SDWebImageManager.h"
@interface SSLaunchAdView ()
@property (nonatomic, strong) UIImageView *bg_imageView;
@property (nonatomic, strong) NSTimer *myTimer;
@property (nonatomic, assign) NSInteger timeCount;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, assign) BOOL isOutWaitTime;
@property (nonatomic, assign) BOOL isHasImage;
@end
@implementation SSLaunchAdView
- (id)initWithFrame:(CGRect)frame AdTime:(NSInteger)AdTime waitTime:(NSInteger)waitTime onView:(UIView *)onView{
if (self = [super initWithFrame:frame]) {
self.AdTime = AdTime;
self.waitTime = waitTime;
[onView addSubview:self];
_bg_imageView = [UIImageView new];
[self addSubview:_bg_imageView];
[self->_bg_imageView setImage:[UIImage imageNamed:[self getLaunchImageName]]];
_imageView = [UIImageView new];
// _imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:_imageView];
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setTitle:@"跳过" forState:0];
_button.titleLabel.font = [UIFont systemFontOfSize:12];
[_button setTitleColor:[UIColor whiteColor] forState:0];
_button.backgroundColor = [UIColor blackColor];
[self addSubview:_button];
[_button setHidden:YES];
[_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
_imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapImageView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageView)];
[_imageView addGestureRecognizer:tapImageView];
if (waitTime>0) {
[self performSelector:@selector(waitAction) withObject:nil afterDelay:waitTime+0.2];
}
}
return self;
}
- (void)layoutSubviews{
[super layoutSubviews];
self.frame = CGRectMake(0, 0, CGRectGetWidth(self.superview.frame), CGRectGetHeight(self.superview.frame));
_bg_imageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.superview.frame), CGRectGetHeight(self.superview.frame));
_imageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.superview.frame), CGRectGetHeight(self.superview.frame));
if (@available(iOS 11.0, *)){
_button.frame = CGRectMake(CGRectGetWidth(_imageView.frame)-50-20, _button.superview.safeAreaInsets.top+20, 50, 28);
}else{
_button.frame = CGRectMake(CGRectGetWidth(_imageView.frame)-50-20, 37, 50, 28);
}
}
- (void)buttonAction:(UIButton *)button{
[self hideView];
}
- (void)waitAction{
@synchronized (self) {
if (!self.isHasImage) {
self.isOutWaitTime = YES;
[self hideView];
}
}
}
- (void)tapImageView{
// [self hideView];
if (self.clickImageView) {
self.clickImageView(self);
}
}
- (NSString *)getLaunchImageName
{
CGSize viewSize = [[UIScreen mainScreen] bounds].size;
// 竖屏
NSString *viewOrientation = @"Portrait";
NSString *launchImageName = nil;
NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
for (NSDictionary* dict in imagesDict)
{
CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]])
{
launchImageName = dict[@"UILaunchImageName"];
}
}
return launchImageName;
}
- (void)setImageUrl:(NSString *)imageUrl{
_imageUrl = imageUrl;
if (!_imageUrl) {
[self hideView];
return;
}
__weak typeof(self) weakSelf = self;
[[SDWebImageManager sharedManager]loadImageWithURL:[NSURL URLWithString:_imageUrl] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
__strong typeof(self) strongSelf = weakSelf;
if (image&&finished&&!self.isOutWaitTime) {
strongSelf.imageView.image = image;
strongSelf.isHasImage = YES;
[strongSelf addTimer];
}
}];
}
- (void)hideView{
[self removeTimer];
[UIView animateWithDuration:0.2 animations:^{
self.alpha = 0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
- (void)addTimer{
_myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop]addTimer:_myTimer forMode:NSRunLoopCommonModes];
if (self.imageView.image) {
[_button setHidden:NO];
}
}
- (void)removeTimer{
[_myTimer invalidate];
_myTimer = nil;
}
- (void)timerAction{
_timeCount ++;
if (_timeCount==self.AdTime) {
[self hideView];
}else{
[_button setTitle:[NSString stringWithFormat:@"%ld跳过",self.AdTime-_timeCount] forState:0];
}
}
- (void)clickImageView:(void (^)(SSLaunchAdView *))block{
self.clickImageView = block;
}
复制代码