ios開發之--新手引導頁的添加

來源:互聯網
上載者:User

標籤:add   lstat   loop   eth   write      launch   不顯示   lines   

以往在寫啟動頁面的時候,有時候會直接在啟動頁裡面寫,或者內建的vc裡面直接寫,但是那樣並不是很方便,啟動頁裡面往往會添加很多的東西,所以封裝成一個單獨的類,可以直接使用,即便是後期的更換,或者是其他的工程項目裡面需要,直接拖過去,就可以直接使用非常方便!

具體代碼就不上傳了,附demo的:

https://github.com/hgl753951/launchTest.git

早先在cocoachina上上傳的一個demo:

http://code.cocoachina.com/view/131777

比較簡單,可以直接下載demo來看!

 

下面寫一個帶有倒計時的廣告頁功能:

1,新建立一個整合於UIView的類:

@interface hDisplayView : UIView

2,.m檔案

a,準備

@interface hDisplayView (){    NSInteger imgTag;    NSString *picURl;    UIImageView *imageView;    UIImageView *smallImg;    UIButton *timerBtn;        int secondsCountDown; //倒計時總時間長度    NSTimer *countDownTimer;}
@property(nonatomic,strong)NSFileManager *fileManager;@property(nonatomic,strong)NSFileHandle *writeHandle;@property(nonatomic,assign)long long sumLength;@property(nonatomic,assign)long long currentLength;@property(nonatomic,strong)UIImage *savedImage;

b,具體實現

@implementation hDisplayView-(instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];        if (self) {        imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, MainScreen_width, MainScreen_height)];//大背景圖片                if (IS_IPHONE4 == YES) {            imageView.image = [UIImage imageNamed:@"start_ios-1"];        }else        {            imageView.image = [UIImage imageNamed:@"start_ios"];        }                imageView.userInteractionEnabled = YES;        [self addSubview:imageView];                smallImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, MainScreen_width, MainScreen_height-107)];//        smallImg.image = [UIImage imageNamed:@"start-ad"];        [imageView addSubview:smallImg];                timerBtn = [[UIButton alloc]initWithFrame:CGRectMake(MainScreen_width - 13 - 48, 13, 48, 48)];                [self initView];    }    return self;}-(void)initBtn{        timerBtn.clipsToBounds = YES;    timerBtn.layer.cornerRadius = 24;    [timerBtn setTitleColor:RGB(255, 221, 0) forState:UIControlStateNormal];    timerBtn.titleLabel.font = [UIFont systemFontOfSize:12];    [timerBtn addTarget:self action:@selector(jumpBtnClick:) forControlEvents:UIControlEventTouchUpInside];    [imageView addSubview:timerBtn];    timerBtn.titleLabel.numberOfLines = 2;    timerBtn.titleLabel.textAlignment = NSTextAlignmentCenter;    timerBtn.backgroundColor = RGBA(29, 29, 29, 0.5);       //開始倒計時    countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES]; //啟動倒計時後會每秒鐘調用一次方法 timeFireMethod    [[NSRunLoop mainRunLoop] addTimer:countDownTimer forMode:NSDefaultRunLoopMode];    //[NSThread detachNewThreadSelector:@selector(starTimer) toTarget:self withObject:nil];            //設定倒計時顯示的時間    //設定倒計時總時間長度    secondsCountDown = 5;//60秒倒計時    [timerBtn setTitle:[NSString stringWithFormat:@"跳過\n%ds",secondsCountDown] forState:UIControlStateNormal];    //     NSTimeInterval period = 1.0; //設定時間間隔//     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), period * NSEC_PER_SEC, 0); //每秒執行//     dispatch_source_set_event_handler(_timer, ^{//              //在這裡執行事件//         if (secondsCountDown<=0) {//             dispatch_source_cancel(_timer);//             dispatch_async(dispatch_get_main_queue(), ^{//                 self.hidden = YES;//             });////         }else{//             dispatch_async(dispatch_get_main_queue(), ^{//            //                 if (secondsCountDown==0) {//                     self.hidden = YES;//                 }else{//                    [timerBtn setTitle:[NSString stringWithFormat:@"跳過\n%ds",secondsCountDown] forState:UIControlStateNormal]; //                 }//             });//             secondsCountDown--;//         }//         NSLog(@"迴圈執行");//         //[self timeFireMethod];//         });//     dispatch_resume(_timer);        }-(void)initView{    //先進行判斷,1:如果是第一次啟動不顯示此啟動圖片,2:如果不是第一次啟動,那麼載入此啟動圖片,如果圖片不存在就下載,如果圖片存在就讀取緩衝    hUser *huser = [[hUser alloc]init];    [hHttpEngine getStartupPicRequest:huser success:^(id response) {        NSLog(@"respons  ----%@",response);        NSDictionary *dict = (NSDictionary *)response;        NSString *stautes = [NSString stringWithFormat:@"%@",[dict objectForKey:@"status"]];        if ([stautes isEqualToString:@"1"]) {            picURl = [NSString stringWithFormat:@"%@",[dict objectForKey:@"pic"]];            NSLog(@"picurl is %@",picURl);            [smallImg sd_setImageWithURL:[NSURL URLWithString:picURl] placeholderImage:[UIImage imageNamed:@""]];            smallImg.userInteractionEnabled = YES;            [self initBtn];        }    }failure:^(NSError *err) {        self.hidden = YES;    }];    }-(void)jumpBtnClick:(id)sender{    self.hidden = YES;}-(void)timeFireMethod{        //倒計時-1    secondsCountDown--;    //修改倒計時標籤現實內容    [timerBtn setTitle:[NSString stringWithFormat:@"跳過\n%ds",secondsCountDown] forState:UIControlStateNormal];        //當倒計時到0時,做需要的操作,比如驗證碼到期不能提交    if(secondsCountDown==0){        [countDownTimer invalidate];        self.hidden = YES;    }}@end

具體效果就不上傳了,可以直接複製上面的代碼,自行運行查看效果;

c,在AppDelegate裡面添加,就是給當前window添加一個根視圖:

    hDisplayView *hVC = [[hDisplayView alloc] initWithFrame:CGRectMake(0, 0, MainScreen_width,  MainScreen_height)];    hVC.hidden = NO;    [self.window.rootViewController.view addSubview:hVC];    [self.window bringSubviewToFront:hVC];

 

搞定,這兩個效果可以結合用,判斷第一次運行app,引導頁出現,反之則出現廣告業!

ios開發之--新手引導頁的添加

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.