- @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
来源: http://www.bubuko.com/infodetail-2291222.html