
2017-08-08 &nbs★α p; | 發布者:梁國(guó÷απ)芳 | &↓✔•nbsp; 查看(kàn):3320次
ios看(kàn)到(dào)現(xiàn)在的(de)各式各樣的(de)應$★用(yòng),好(hǎo)多(duō)應用←α<(yòng)都(dōu)可(kě)以每天更換啓動頁廣告,于是(shì)★↓™決定自(zì)己也(yě)做(zuò)一(yī)個(gè)。
首先打開(kāi)AppDelegate.m
大(dà)家(jiā)都(dōu)知(zhī)道(×¥₩ dào)啓動頁在加載完畢會(huì)自(zì)動執行(xíng)AppDele♣☆εgate didFinishLaunchingWithOpt∞Ωβ¶ions 函數(shù):
- (BOOL)application:(UIA≤↑∏→pplication *)applica×★tion didFinishLaunchingWithOε∏↕≠ptions:(NSDictionary *)launchOpt§ ₹ions { ≥$
// Ov♠÷erride point for customδ×↓ ization after application&n φ"bsp;launch.
&n>β>§bsp; r↔₹∑eturn YES;
}所以直接将加載廣告代碼寫在這(zhè)個(gè)函數(shù)裡(lǐ)¥♦面就(jiù)對(duì)了(le):
#import "AppDeleg ©♥♣ate.h" @interface&n€♠bsp;AppDelegate ()  $; //啓動頁view &₩€nbsp;@property (strong, no>↓natomic) UIView λ∑$;*LaunchView; @end &€★✘nbsp;@implementation "φπAppDelegate &nbs✘σ ₩p;- (BOOL)application:(UIApp™βlication *)application did&•>£FinishLaunchingWithOptו₩±ions:(NSDictionary *)←≤α✔launchOptions { &n¥γbsp;
/φ₹/ Override poi✔♣¥nt for customizati®λon after applicatiβ≠☆on launch.
→↕≥∑ //創建✔♥∑window
&n₹'bsp; self.window €↔§β;= [[UIWindow&nbσ♣₹sp;alloc] initWithF →♦∏rame:[[UIScreen mainScreen] ×☆bounds]];
&nbs$π®p; self.window.backgroundColor♥λα< = [UIColor whiteCo©™₹↑lor];
 σ$; //獲取啓動頁 &nb§¥ ÷sp;
&♦π↕☆nbsp;UIStoryboard &Ω*LaunchScreen =©≥> [UIStoryboard st ©≠oryboardWithName:@"LaunchS∏™↑←creen" bundle•÷☆:nil];
&nπ★¥πbsp; UIViewController ♠₹λ;*LaunchVC = [λδ≠λLaunchScreen instantiateInitialV£δ₩>iewController]; &nb∞ sp;
&nbs₹®↕¥p;self.LaunchView = L€&aunchVC.view;
&nbs£∞ p; //加★★載遠(yuǎn)程廣告
 ¥₹→; [self loadAD]; ♦∑δ↕
/&≥∏/設置rootView
 •≥₹; self.¶ε§window.rootViewController& •✘♠nbsp;= LaunchVC; γε&
[se•ε£♦lf.window makeKeyAndVisi ↑ble];
&nb↕δ©₹sp; return YES; ★ →;
} #pragma☆ε₹ mark 加載遠(yuǎn)程廣告 &nb™™εsp;- (void)loadAD &nb↑&sp;
{
&nb<sp; //遠(yuǎn)程圖片地(dì)址&nb♣α¶sp;
&™φnbsp; NSURL *url =&nb✔σsp;[NSURL URLWithString:@"∏φhttps://timgsa.baidu.com/timg?i→✘®mage&quality=80&a"₩←mp;size=b9999_10000&am→≤p;sec=1486466389044&di↔'≈✘=a7ab421bb82bd0d214bbdc5a6b79f2bf&a✘γ★mp;imgtype=0&src=http%3A%2F%2Fpi'✘c.qiantucdn.com%2F58pic%2F17%2F80%2$₽F57%2F94s58PICA7j_1024.jpg"];&nbs↓₩→p;
&nbs€©Ω✘p; //異步加載圖片
&nb↔$Ω♦sp; dispatcπα'>h_queue_t queue = d♦λispatch_queue_create(&q£π↔uot;loadImage", NULL);&↓≠nbsp;
&n→∑↔bsp; dispatch_async(quγ§•♣eue, ^{ ←≠≤β
&nb♠≈→sp; //圖片資源&<☆nbsp;
  '♦;  ≤©; NSData&nb™₩sp;*imgData = [NS¶φ®σData dataWithCon>✔tentsOfURL:url]; &nbσββ♥sp;
 ≤≥δ; UIIm∞'•age *img = [UIImage£≤ imageWithData:imgData←÷<];
λ> &nb¶↔★sp;dispatch_async(disp≈≠atch_get_main_queue(), ^{←↓∏
&nbs←ε☆p; &☆≤nbsp; £¥↕≈ //創建imageView
÷ ∏
&nbλ↓sp;   ≤; UIImageView *adVieλ↕&σw = [[UIImagδ&® eView alloc] initWithFrame:↓•♣εCGRectMake(0, 0, [[UIScree♦☆&™n mainScreen] boun≤∑ds].size.width, [[UIScreσ☆φ≥en mainScreen]&nα¥bsp;bounds].size.height)];&nbs∑✔↑p;
 ↕₽; &nα••bsp; adView★✔↓.image = img;&n∏×πbsp;
&nb££sp;  ≠α; &nb₽sp;//添加圖片點擊手勢 &nε¥€bsp;
&nb✘↓γsp; &•ε"♥nbsp; &nbs♣™p;UITapGestureRecogni•α$zer *singleTap = [[UI™♦δTapGestureRecognizer alloc] ™Ω;initWithTarget:self acti☆↔ ↕on:@selector(clickAD)]; &≥×♠nbsp;
&n ±βbsp;  €; adView.u∏↓&serInteractionEnabled&nb✘Ωsp;= YES;
&nb¶sp;  <∏≥; &δ♣£₽nbsp;[adView addGestureRecogαφnizer:singleTap];
&nb✔∏sp; ±ε↑ &≈®♣nbsp;[self.LaunchView&nbs¥<"βp;addSubview:adView]; &nΩ✘bsp;
&σ'♦ nbsp; &nb>×↕sp; &nbs>™σπp;//LaunchView前置于window&§©nbsp;
&nbs×±p;  πδ®; [se±₹∏lf.window bringSubviewToFront:selfα×.LaunchView];
&n×↔bsp; &nb☆$★sp; &nb ββλsp; //延遲5秒(miǎo₹↑₽φ)後删除廣告
&nbs☆<©±p; &§ ®nbsp; &nbs∑★&∏p;[self performSel↔>ector:@selector(removeAd) withObj Ωect:nil afterDelay:5]; &♥$ nbsp;
&n→£≈≥bsp; &nbs♣≤p;});
 <π ✘; }); ←' ←
} #pragma mark&n•βλbsp;廣告點擊 -&nbπ≠σ♥sp;(void)clickAD&nbs p;{
 ♠♥'; [self&nbs§≈p;removeAd];
}♥∏↕₹ #pragma ÷§;mark 删除廣告 - (v$πoid)removeAd
{ &nbs α£♠p;
&nbs ↕♠≠p; [self.LaunchVi ≤ew removeFromSuperview];&n¶↔$bsp;
&nbs™™p;//重新設置rootView ♣∏☆
&©↓nbsp;UIStoryboard&nbsσ±p;*main = [UIStoryboard&nb÷↑sp;storyboardWithName:@"Main&q↓ uot; bundle:nil];&nπ♣bsp;
&☆↓nbsp;self.window.rootViewContα™roller = [main i¥©nstantiateInitialViewCoπntroller];
&nb> ↕sp; [s♣ ↕>elf.window makeKeyAndVisible];&nbs☆Ω≠p;
}看(kàn)看(kàn)效果:

