启动图后面 添加有倒计时的广告图

今天闲来无事,看到很多APP在启动图后面添加了广告图,今天就动手做了一下,

刚开始,打算用通知在APPdelegate中添加这个图片,但是尝试后发现,效果不是很理想;就在rootVC中进行操作了

直接上代码:

-(void)gotoImage
{
     self.window =  [[UIApplication sharedApplication].delegate window];
    
    self.mianImageview = [[UIImageView alloc]init];
    _mianImageview.image = [UIImage imageNamed:@"mianimage.jpg"];
    _mianImageview.frame = _window.bounds;
    _mianImageview.tag = 100;
    _mianImageview.userInteractionEnabled = YES;
    [_window addSubview:_mianImageview];
    
    UITapGestureRecognizer *pan = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickmianimage)];
    pan.numberOfTouchesRequired = 1;
    pan.numberOfTapsRequired = 1;
    [_mianImageview addGestureRecognizer:pan];
    
    
    //创建UILabel 添加到当前view
    _labelText=[[UILabel alloc]initWithFrame:CGRectMake(40, MYScreenH-100, 80, 30)];
    _labelText.backgroundColor = [UIColor redColor];
    _labelText.textAlignment = NSTextAlignmentCenter;
    [_mianImageview addSubview:_labelText];
    
    //设置倒计时总时长
    _secondsCountDown = 2;//60秒倒计时
    //开始倒计时
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES]; //启动倒计时后会每秒钟调用一次方法 timeFireMethod
    
    //设置倒计时显示的时间
    _labelText.text=[NSString stringWithFormat:@"%ldS",(long)_secondsCountDown];
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        for (UIView *subviews in [_window subviews]) {
            if (subviews.tag ==100) {
    
                    [UIView animateWithDuration:1.0 animations:^{
                        
                        CGPoint center = _mianImageview.center;
                        _mianImageview.width = 10000;
                        _mianImageview.height = 10000;
                        _mianImageview.center = center;
                        
                        subviews.alpha = 0;
                        
                    } completion:^(BOOL finished) {
                        
                        [subviews removeFromSuperview];
                        
                    }];
                }
            
            }
        

    });
}

这个方法在viewdidload:中调用;

倒计时的方法,每秒就调用一次:

-(void)timeFireMethod{
    //倒计时-1
    _secondsCountDown--;
    //修改倒计时标签现实内容
    _labelText.text=[NSString stringWithFormat:@"%ldS",(long)_secondsCountDown];
    //当倒计时到0时,做需要的操作,比如验证码过期不能提交
    if(_secondsCountDown==-1){

        [_countDownTimer invalidate];
        [_labelText removeFromSuperview];
    }
}

下面的这个方法是这个广告图的点击方法,一般是跳到广告详情的,我在这里只是提前隐藏广告图

-(void)clickmianimage
{
        for (UIView *subviews in [_window subviews]) {
            if (subviews.tag ==100) {
                
                [UIView animateWithDuration:0.5 animations:^{
                    
                    CGPoint center = _mianImageview.center;
                    _mianImageview.width = 10000;
                    _mianImageview.height = 10000;
                    _mianImageview.center = center;
                    

                    subviews.alpha = 0;
                    
                } completion:^(BOOL finished) {
                    
                    [subviews removeFromSuperview];
                    
                }];
            }
            
        }
    

}

如果大神发现有什么问题,求在评论中指出:谢谢指教!

原文地址:https://www.cnblogs.com/liuwenqiang/p/5653020.html