app啟動廣告頁的實現,解決了廣告圖片要實時更新的問題

網上很多的實現方法很多都是顯示第一次的緩存的圖片,這樣就造成后臺更新廣告圖片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)frameAdTime:(NSInteger)AdTimewaitTime:(NSInteger)waitTimeonView:(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;
}復制代碼

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/281922.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/281922.shtml
英文地址,請注明出處:http://en.pswp.cn/news/281922.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

vue中點擊插入html_Vue中插入HTML代碼的方法

我們需要吧Hello World插入到My name is Pjee應該如何做?一、使用v-htmlv-html:更新元素的 innerHTMLconst text Hello World>My name is Pjee注意:你的站點上動態渲染的任意 HTML 可能會非常危險,因為它很容易導致 XSS 攻擊。請只對可信…

進程共享變量#pragma data_seg用法

#pragma data_seg介紹用#pragma data_seg建立一個新的數據段并定義共享數據,其具體格式為:   #pragma data_seg ("shareddata")   HWND sharedwndNULL;//共享數據   #pragma data_seg() ---------------------------------…

機器視覺Halcon教程(1.介紹)

前言本期教程主要教大家如何使用Halcon機器視覺,通過使用Halcon, 我們可以實現一些機器視覺的應用開發。例如: OCR識別、視覺定位、缺陷檢測等內容。什么是halcon?簡單來說, Halcon就是一款應用于機器視覺的軟件,它提供了一套開發工具&#x…

網絡時間的那些事及 ntpq 詳解

2019獨角獸企業重金招聘Python工程師標準>>> GMT (Greenwich Mean Time)格林威治時間 UTC (Coordinated Universal Time) 協調世界時 IAT (International Atomic Time),TAI 國際原子時 CST (Chinese Standard Time), 北京時間Gentoo(也許其他發行版也是&…

【前端芝士樹】Javascript的原型與原型鏈

【前端芝士樹】Javascript的原型、原型鏈以及繼承機制 前端的面試中經常會遇到這個問題,自己也是一直似懂非懂,趁這個機會整理一下0. 為什么會出現原型和原型鏈的概念 1994年,網景公司(Netscape)發布了Navigator瀏覽器…

神奇的幻方2015提高組d1t1

題目描述 幻方是一種很神奇的N*N矩陣:它由數字1,2,3,……,N*N構成,且每行、每列及兩條對角線上的數字之和都相同。 當N為奇數時,我們可以通過以下方法構建一個幻方: 首先將1寫在第一行的中間。 之后,按如下方式從小到大…

goldengate mysql_使用GoldenGate實現MySQL到Oracle的數據實時同步

step 1: 配置mysql修改配置文件my.ini#for goldengatelog-bin "C:/mysql/logbin/logbin.log"binlog-format ROWlog-bin-index "C:\mysql\logindex"binlog_cache_size32mmax_binlog_cache_size512mmax_binlog_size512m添加數據庫用戶ggs,具有…

C# 反射之Activator用法舉例

概述程序運行時,通過反射可以得到其它程序集或者自己程序集代碼的各種信息,包括類、函數、變量等來實例化它們,執行它們,操作它們,實際上就是獲取程序在內存中的映像,然后基于這個映像進行各種操作。Activa…

MyBatis批量插入

轉載于:https://blog.51cto.com/12701034/1929672

狐貍文│區塊鏈發展的正路

(圖片出自網絡,版權歸原作者所有)最近看了一本書:《美國增長的起落》。這本書是大部頭,但看起來很過癮。通過對這本書的閱讀,我更新了自己對區塊鏈發展的理解。這一年,“區塊鏈”很熱&#xff0…

mysql 一主一備_Mysql一個主一備

Mysql主從復制 -- 一主一備主從復制原理:Mysql的主從復制是mysql本身自帶的一個功能,不需要額外的第三方軟件可以實現,其復制功能并不是copy文件實現的,而是借助binlog日志文件里面的SQL命令實現的主從復制,可以理解為…

解決安裝Weblogic domain卡住問題(Primeton BPS)

這兩天一直有一個問題困擾我,在suse10weblogic(920,923,100,103)上安裝bpm產品失敗。有些版本是創建domain的時候卡在create security information上,有些版本卡在安裝包start weblogic上。但是在winXPweblogic10.3bpm安裝成功。 經過幾番GOOGLE,終于找到…

cocos2d-js 熱更新具體解釋(一)

本文將會具體解說cocos2d-js下的熱更新機制。這篇內容先給大家介紹一下兩個manifest文件就當熱身了。首先介紹project.manifest: 舉個樣例 {"packageUrl" : "http://192.168.1.108/games/dragon_gold","remoteManifestUrl" : "…

Qt之水平/垂直布局(QBoxLayout、QHBoxLayout、QVBoxLayout)

簡述 QBoxLayout可以在水平方向或垂直方向上排列控件,由QHBoxLayout、QVBoxLayout所繼承。 QHBoxLayout:水平布局,在水平方向上排列控件,即:左右排列。 QVBoxLayout:垂直布局,在垂直方向上排列控…

Optaplanner終于支持多線程并行運行 - Multithreaded incremental solving

Optaplanner 7.9.0.Final之前,啟動引擎開始對一個Problem進行規劃的時候,只能是單線程進行的。也就是說,當引擎對每一個possible solution進行分數計算的過程中,細化到每個步驟(Caculation),都只能排隊在同一個線程中依…

python棋盤格_干貨必看 | Python的turtle庫之經典棋盤格

國際棋盤格是一個由9橫9縱的線組成的格子正方形,用Python的turtle庫進行繪制的時候,先做9橫9縱的線,再填上灰色小正方形,這就可以完成一個棋盤格了,下面是具體的操作步驟。(一)整體代碼1、import turtleimport turtle2…

一位技術老人給.NET初學者的一些建議

.NET平臺應用領域眾多,隨著這些年的不斷更新迭代,日趨臻善,也受到越來越多的開發者青睞。自從2000 年6 月22 日 微軟推出Microsoft.NET 戰略 ,至今已有22載,這些年新技術,新框架層出不窮,目不暇…

android 本地數據庫sqlite的封裝

單機android sqlite數據庫的實現,這個數據庫可與程序一起生成在安裝包中一、下載sqlite3.exe文件二、運行 cmd 轉到sqlite3.exe 所在目錄 運行 sqlite3.exe 數據庫名.db然后會出現sqlite>的命令提示符輸入創建表的語句, create table 表名&#xf…

ResourceManager中的Resource Estimator框架介紹與算法剖析

歡迎大家前往騰訊云社區,獲取更多騰訊海量技術實踐干貨哦~ 本文由宋超發表于云社區專欄 本文首先介紹了Hadoop中的ResourceManager中的estimator service的框架與運行流程,然后對其中用到的資源估算算法進行了原理剖析。 一. Resource Estimator Service…

幾十款 WPF 控件 - UI 庫,總有一款適合你

幾十款 WPF 控件 - UI 庫,總有一款適合你獨立觀察員 2022 年 10 月 16 日引言眾所周知,使用 WPF 框架能夠開發出功能強大、界面美觀的桌面端應用。能夠達到這個效果,各種 WPF 的控件庫、UI 庫功不可沒。所以,想著能不能收集一下目…