GXiOS

iOS广告页的实现及注意点

2018-11-27  本文已影响0人  一个不太努力的代码搬运工
在大多数app启动过程中都会出现广告页,那么现在说说到底是如何实现的。
实现思路:
注意点:

在这里我是用了Masonry SDWebImage这两个框架,具体作用你们都懂得。
在实际项目中我们会用AFN请求网络数据,并且封装成model使用,这里我就直接设置imageUrl了。。。
自定义广告页view
ZSCustomAdvertisementViewController.h

#import <UIKit/UIKit.h>
typedef void (^ CustomAdvertisementViewClickBlock)(void);
typedef void (^ CustomSkipButtonClickBlock)(void);
@interface ZSCustomAdvertisementViewController : UIViewController
///图片链接
@property (nonatomic,copy) NSString *imageUrl;
///持续时间
@property (nonatomic,assign) NSInteger duration;
///是否加载成功
@property (nonatomic,assign) BOOL isLoadSuccess;
///图片点击的block
@property (nonatomic,copy) CustomAdvertisementViewClickBlock imageClickBlock;
///跳过按钮的block
@property (nonatomic,copy) CustomSkipButtonClickBlock skipButtonClickBlock;
@end

ZSCustomAdvertisementViewController.m

#import "ZSCustomAdvertisementViewController.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
@interface ZSCustomAdvertisementViewController ()

///广告图
@property (nonatomic,strong) UIImageView *advertisementImageView;
///倒计时
@property (nonatomic,strong) UIButton *skipButton;
///定时器
@property (nonatomic,strong) NSTimer *timer;
@end

@implementation ZSCustomAdvertisementViewController
#pragma mark -- 懒加载
- (UIImageView *)advertisementImageView {
    if (_advertisementImageView == nil) {
        _advertisementImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
        [self.view addSubview:_advertisementImageView];
        [_advertisementImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:nil];
        [_advertisementImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            [self downloadImage];
        }];
        _advertisementImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageClickBlock:)];
        [_advertisementImageView addGestureRecognizer:tap];
    }
    return _advertisementImageView;
}

- (UIButton *)skipButton {
    if (_skipButton == nil) {
        _skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _skipButton.layer.cornerRadius = 10;
        _skipButton.clipsToBounds = YES;
        _skipButton.layer.shouldRasterize = YES;
        _skipButton.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [_skipButton setTitle:[NSString stringWithFormat:@"%zds跳过",self.duration] forState:UIControlStateNormal];
        _skipButton.backgroundColor = [UIColor clearColor];
        [_skipButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _skipButton.layer.borderColor = [UIColor whiteColor].CGColor;
        _skipButton.layer.borderWidth = 1;
        [_skipButton addTarget:self action:@selector(countButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(countTime) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
    }
    return _skipButton;
}

#pragma mark -- Action
///点击图片跳转webview
-(void)imageClickBlock:(UITapGestureRecognizer *)tap {
    [self invalidatedTimer];
    if (self.imageClickBlock) {
        self.imageClickBlock();
    }
}
///点击跳过按钮
- (void)countButtonClick {
    if (self.skipButtonClickBlock) {
        self.skipButtonClickBlock();
    }
}
///定时器方法
- (void)countTime {
    self.duration --;
    [self.skipButton setTitle:[NSString stringWithFormat:@"%zds跳过",self.duration] forState:UIControlStateNormal];
    if (self.duration <= 0) {
        [self invalidatedTimer];
        //切换根控制器
        if (self.skipButtonClickBlock) {
            self.skipButtonClickBlock();
        }
        return;
    }
}
///销毁定时器
- (void)invalidatedTimer {
    [_timer invalidate];
    _timer = nil;
}

#pragma mark -- 下载图片到本地
- (void)downloadImage {
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *imagePath = [path stringByAppendingPathComponent:@"currentImage.png"];
    [UIImagePNGRepresentation(self.advertisementImageView.image) writeToFile:imagePath atomically:YES];
}
///获取沙盒中图片
- (UIImage *)getDocumentImage {
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *imagePath = [path stringByAppendingPathComponent:@"currentImage.png"];
    return [UIImage imageWithContentsOfFile:imagePath];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.advertisementImageView addSubview:self.skipButton];
    [self.skipButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(80);
        make.right.equalTo(self.advertisementImageView).offset(-30);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(40);
    }];
}

AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    TabViewController *taVc = [[TabViewController alloc]init];
    ZSCustomAdvertisementViewController *vc1 = [[ZSCustomAdvertisementViewController alloc]init];
    self.window.rootViewController = vc1;
    vc1.imageUrl = @"http://img06.tooopen.com/images/20171116/tooopen_sy_228521528924.jpg";
    vc1.duration = 4;
    vc1.skipButtonClickBlock = ^{
        self.window.rootViewController = taVc;
    };
    //图片点击的block执行
    vc1.imageClickBlock = ^{
        self.window.rootViewController = taVc;
//这里尤为重要,要保证发送和接收方都在同一线程
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter]postNotificationName:@"pushController" object:nil];
        });
    };
    [self.window makeKeyAndVisible];
    return YES;
}

最后在首页控制器的viewDidLoad中注册通知,并在通知方法中跳转控制器

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushController) name:@"pushController" object:nil];
    self.title = @"首页";

}

- (void)pushController {
    self.hidesBottomBarWhenPushed = YES;
    SecondViewController *seVc = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:seVc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
效果图
广告页.gif
上一篇下一篇

猜你喜欢

热点阅读