ADView.m 3.54 KB
//
//  ADView.m
//  IVTMeLiveTwo
//
//  Created by XRG on 16/8/12.
//  Copyright © 2016年 QLQ. All rights reserved.
//

#import "ADView.h"

#import "WebViewVC.h"
#import "RootTabBarController.h"
#import "IVTLoginViewController.h"

static ADView *instance = nil;

@implementation ADView{
    NSDictionary *dataDict;
    
}

+ (ADView *)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
        [instance setupView];
    });
    return instance;
}

- (void)setupView{
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    self.backgroundColor = [UIColor whiteColor];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    imageV.backgroundColor = [UIColor whiteColor];
    NSString *adurl = [IVTAccountTool getADUrl];
    if (adurl.length > 0 && adurl != nil) {
        [imageV sd_setImageWithURL:[NSURL URLWithString:[IVTAccountTool getADUrl]] placeholderImage:[UIImage imageNamed:@"LaunchImg"]];
    }
    else{
        imageV.image = [UIImage imageNamed:@"LaunchImg"];
    }

    [self addSubview:imageV];
    
    imageV.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [imageV addGestureRecognizer:tap];

    UIButton *skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
    skipButton.frame  = CGRectMake(kScreenWidth-58, 50, 48, 28);
    [skipButton setBackgroundImage:[UIImage imageNamed:@"跳过按钮"] forState:UIControlStateNormal];
    [skipButton setTitle:@"跳过" forState:UIControlStateNormal];
    [skipButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    skipButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [skipButton addTarget:self action:@selector(skipClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:skipButton];
    
    [[IVTNetworkAPIClient sharedClient] getADImageSuccess:^(NSDictionary *successData) {
        dataDict = [[successData valueForKey:@"list"] firstObject];

        NSString *imageUrl = [[[successData valueForKey:@"list"] firstObject] valueForKey:@"additionData"];
        [IVTAccountTool setADUrl:imageUrl];
    } error:^(NSDictionary *successData) {
        
    } failure:^(NSError *failureError) {
        
    }];
    
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(skipClick) userInfo:nil repeats:NO];
}

- (void)skipClick{
    [UIView animateWithDuration:0.4 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    tap.enabled = NO;
    NSString *urlStr = dataDict[@"conUrl"];
    if (urlStr) {
        RootTabBarController *tabBarVC = [RootTabBarController tabBarVC];
        
        WebViewVC *vc = [[WebViewVC alloc] init];
        vc.webUrl = urlStr;
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        if ([tabBarVC.presentedViewController isKindOfClass:[IVTLoginViewController class]]) {
            [tabBarVC.presentedViewController presentViewController:nav animated:YES completion:^{
            }];
        } else {
            [tabBarVC presentViewController:nav animated:YES completion:^{
            }];
        }
    }

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end