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


以往在寫啟動頁面的時候,有時候會直接在啟動頁里面寫,或者自帶的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,引導頁出現,反之則出現廣告業!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM