啟動(dòng)頁(yè)

一靠欢、AppDelegate.m里面引入類(lèi)ViewController.h然后設(shè)置首頁(yè)面

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

? ? ? ? self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

? ? ? ? ViewController *mainVC=[[ViewController alloc]init];

? ? ? ? ?UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:mainVC];

? ? ? ? ?self.window.rootViewController = nav;

? ? ? ? ?[self.window makeKeyAndVisible];

? ? ? ? return YES;

}

二门扇、新建FlashPageView類(lèi)繼承UIView揽思,用來(lái)設(shè)置啟動(dòng)頁(yè)

1炮温、FlashPageView.h

#define kscreenWidth [UIScreen mainScreen].bounds.size.width

#define kscreenHeight [UIScreen mainScreen].bounds.size.height

#import ?<UIKit/UIKit.h>

typedef void (^PushToAdBlock)(void);

@interface FlashPageView : UIView

@property (nonatomic, strong) UIImageView *adView;/** 圖片*/

@property (nonatomic, copy) NSString *filePath;/** 圖片路徑*/

@property (nonatomic,copy) PushToAdBlock ?pushToAdBlock;

- (void)show;/** 顯示啟動(dòng)頁(yè)面方法*/

-(void) TouchPushToAdBlock:(PushToAdBlock)pushToAdBlock;

@end

2横侦、FlashPageView.m

#import "FlashPageView.h"

@interface FlashPageView ()

@property (nonatomic, strong) UIButton *countBtn; ?/**倒計(jì)時(shí)按鈕*/

@property (nonatomic, strong) NSTimer *countTimer;?/**定時(shí)器*/

@property (nonatomic, assign) int count; ?/**倒計(jì)時(shí)*/

@end

static int const showtime = 10; ?// 啟動(dòng)頁(yè)顯示的時(shí)間

@implementation?FlashPageView

- (instancetype)initWithFrame:(CGRect)frame{

? ? ? ? if (self = [super initWithFrame:frame]) {

? ? ? ? // 1.閃頻頁(yè)圖片

? ? ? ? _adView = [[UIImageView alloc] initWithFrame:frame];

? ? ? ?_adView.image = [UIImage imageNamed:@"啟動(dòng)頁(yè)"];

? ? ? ?_adView.userInteractionEnabled = YES;//用戶交互

? ? ? ?_adView.contentMode = UIViewContentModeScaleAspectFill;//?等比例縮放至父容器完全裝下圖片為止.圖片沒(méi)有變形

? ? ? ? _adView.clipsToBounds = YES;

? ? ? ? ?UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToAd)]; ?//利用手勢(shì)給控件添加點(diǎn)擊事件

? ? ? ? [_adView addGestureRecognizer:tap];

? ? ? ? // 2.跳過(guò)按鈕

? ? ? ? CGFloat btnW = 60;

? ? ? ? CGFloat btnH = 30;

? ? ? ? _countBtn = [[UIButton alloc] initWithFrame:CGRectMake(kscreenWidth - btnW - 24, btnH, btnW, btnH)];

? ? ? ? [_countBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];

? ? ? ? [_countBtn setTitle:[NSString stringWithFormat:@"跳過(guò)%d", 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;

? ? ? ? ?[_countBtn addTarget:self action:@selector(countBtnClick) forControlEvents:UIControlEventTouchUpInside];

? ? ? ? ?[self addSubview:_adView];

? ? ? ? ?[self addSubview:_countBtn];

? ? ? ? }

? ? ? ? return self;

}

- (NSTimer *)countTimer{

? ? ? ? ? if (!_countTimer) {

? ? ? ? ? ? ? ? ? ?_countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];

? ? ? ? ?}

? ? ? ? ?return _countTimer;

}

-(void)countBtnClick{

? ? ? ? ?[self dismiss];

}

- (void)setFilePath:(NSString *)filePath{

? ? ? ? _filePath = filePath;

? ? ? ?_adView.image = [UIImage imageWithContentsOfFile:filePath];

}

- (void)pushToAd{

? ? ? ?NSLog(@"點(diǎn)擊圖片");

? ? ? ? __weak typeof(self) WeakSelf = self;

? ? ? ?if (WeakSelf.pushToAdBlock) {

? ? ? ? ? ? ? ?WeakSelf.pushToAdBlock();

? ? ? ? }

? ? ? ? ?[self dismiss];

}

-(void) TouchPushToAdBlock:(PushToAdBlock)pushToAdBlock{

? ? ? ? ?self.pushToAdBlock = pushToAdBlock;

}

- (void)countDown{

? ? ? ? ?_count --;

? ? ? ? ?[_countBtn setTitle:[NSString stringWithFormat:@"跳過(guò)%d",_count] forState:UIControlStateNormal];

? ? ? ? if (_count == 0) {

? ? ? ? ? ? ? ? [self.countTimer invalidate];

? ? ? ? ? ? ? ? self.countTimer = nil;

? ? ? ? ? ? ? ? [self dismiss];

? ? ? ? ? }

}

- (void)show{

? ? ? ? ? ? // 倒計(jì)時(shí)方法1:GCD

? ? ? ? ? ? //? ? [self startCoundown];

? ? ? ? ? ? // 倒計(jì)時(shí)方法2:定時(shí)器

? ? ? ? ? ? ? [self startTimer];

? ? ? ? ? ? ? UIWindow *window = [UIApplication sharedApplication].keyWindow;

? ? ? ? ? ? ?[window addSubview:self];

}

// 定時(shí)器倒計(jì)時(shí)

- (void)startTimer{

? ? ? ? ? ?_count = showtime;

? ? ? ? ? ?[[NSRunLoop mainRunLoop] addTimer:self.countTimer forMode:NSRunLoopCommonModes];//定時(shí)器只運(yùn)行在NSDefaultRunLoopMode下彻犁,一旦RunLoop進(jìn)入其他模式局待,這個(gè)定時(shí)器就不會(huì)工作斑响,該模式表示在沒(méi)有輸入源的情況下

}

//// GCD多線程倒計(jì)時(shí)

//- (void)startCoundown{

//? ? __block int timeout = showtime + 1; //倒計(jì)時(shí)時(shí)間 + 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); //每秒執(zhí)行

//? ? dispatch_source_set_event_handler(_timer, ^{

// ? ?if(timeout <= 0){ //倒計(jì)時(shí)結(jié)束,關(guān)閉

//? ? ? ? ? ? dispatch_source_cancel(_timer);

//? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

//? ? ? ? ? ? ? ? [self dismiss];

//? ? ? ? ? ? });

//? ? ? ? }else{

//? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

// ? ? ? ? ? ? ?[_countBtn setTitle:[NSString stringWithFormat:@"跳過(guò)%d",timeout] forState:UIControlStateNormal];

//? ? ? ? ? ? });

//? ? ? ? ? ? timeout--;

//? ? ? ? }

//? ? });

//? ? dispatch_resume(_timer);

//}

// 移除閃頻頁(yè)面

- (void)dismiss{

? ? ? ? ? ? [UIImageView animateWithDuration:2.0 animations:^{

? ? ? ? ? ? ? ? ? ? ?self.adView.alpha = 0;

? ? ? ? ? ? ? ? ? ? ?[_countBtn removeFromSuperview];

? ? ? ? ? ? ? ? ? ? ?[self.adView removeFromSuperview];

? ? ? ? ? ? } completion:^(BOOL finished) {

? ? ? ? ? ? ? ? ? ? [self removeFromSuperview];

? ? ? ? ? ? }];

}

三钳榨、在ViewController.m里面引入類(lèi)FlashPageView.h然后調(diào)用方法

- (void)viewDidLoad {

? ? ? ? ? ? [super viewDidLoad];

? ? ? ? ? ? self.view.backgroundColor = [UIColor whiteColor];

? ? ? ? ? ? self.title = @"首頁(yè)";

? ? ? ? ? ? FlashPageView *flashPage?= [[FlashPageView alloc]initWithFrame:self.view.bounds];

? ? ? ? ? ? [flashPage show];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末舰罚,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子薛耻,更是在濱河造成了極大的恐慌营罢,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,539評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件饼齿,死亡現(xiàn)場(chǎng)離奇詭異饲漾,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)缕溉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評(píng)論 3 396
  • 文/潘曉璐 我一進(jìn)店門(mén)考传,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人倒淫,你說(shuō)我怎么就攤上這事伙菊。” “怎么了敌土?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,871評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵镜硕,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我返干,道長(zhǎng)兴枯,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,963評(píng)論 1 295
  • 正文 為了忘掉前任矩欠,我火速辦了婚禮财剖,結(jié)果婚禮上悠夯,老公的妹妹穿的比我還像新娘。我一直安慰自己躺坟,他們只是感情好沦补,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評(píng)論 6 393
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著咪橙,像睡著了一般夕膀。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上美侦,一...
    開(kāi)封第一講書(shū)人閱讀 51,763評(píng)論 1 307
  • 那天产舞,我揣著相機(jī)與錄音,去河邊找鬼菠剩。 笑死易猫,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的具壮。 我是一名探鬼主播准颓,決...
    沈念sama閱讀 40,468評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼嘴办!你這毒婦竟也來(lái)了瞬场?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤涧郊,失蹤者是張志新(化名)和其女友劉穎贯被,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體妆艘,經(jīng)...
    沈念sama閱讀 45,850評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡彤灶,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評(píng)論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了批旺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片幌陕。...
    茶點(diǎn)故事閱讀 40,144評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖汽煮,靈堂內(nèi)的尸體忽然破棺而出搏熄,到底是詐尸還是另有隱情,我是刑警寧澤暇赤,帶...
    沈念sama閱讀 35,823評(píng)論 5 346
  • 正文 年R本政府宣布心例,位于F島的核電站,受9級(jí)特大地震影響鞋囊,放射性物質(zhì)發(fā)生泄漏止后。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望译株。 院中可真熱鬧瓜喇,春花似錦、人聲如沸歉糜。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,026評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)现恼。三九已至肃续,卻和暖如春黍檩,著一層夾襖步出監(jiān)牢的瞬間叉袍,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,150評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工刽酱, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留喳逛,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,415評(píng)論 3 373
  • 正文 我出身青樓棵里,卻偏偏與公主長(zhǎng)得像润文,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子殿怜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評(píng)論 2 355