APP每次启动都会展示广告启动页:
LSLaunchAD.show(with: self.window, countTime: 5, showCountTimeOfButton: true, showSkipButton: true, isFullScreenAD: true, localAdImgName: nil, imageURL: "http://yun.kchuangqi.com/uploads/images/Advertisement_chuangqi.png", canClickAD: true) { (clickAD) in
print("是否点击广告---->\(clickAD)")
if clickAD {
print("点击了广告页")
let adV = WebVC()
(self.tabVC.viewControllers!.first as! UINavigationController).pushViewController(adV, animated: true)
return
} else {
print("----完成倒计时或点击了跳转按钮")
}
}
源码实现原理:
.h文件实现:
#import <UIKit/UIKit.h>
#define kScreen_height [[UIScreen mainScreen] bounds].size.height
#define kScreen_width [[UIScreen mainScreen] bounds].size.width
@interface LaunchIntroductionView : UIView
/**
* 选中page的指示器颜色,默认白色
*/
@property (nonatomic, strong) UIColor *currentColor;//
/**
* 其他状态下的指示器的颜色,默认
*/
@property (nonatomic, strong) UIColor *nomalColor;
/**
* 不带按钮的引导页,滑动到最后一页,再向右滑直接隐藏引导页
*
* @param imageNames 背景图片数组
*
* @return LaunchIntroductionView对象
*/
+(instancetype)sharedWithImages:(NSArray *) imageNames;
/**
* 带按钮的引导页
*
* @param imageNames 背景图片数组
* @param buttonImageName 按钮的图片
* @param frame 按钮的frame
*
* @return LaunchIntroductionView对象
*/
+(instancetype)sharedWithImages:(NSArray *) imageNames buttonImage:(NSString *) buttonImageName buttonFrame:(CGRect ) frame;
/**
用storyboard创建的project调用此方法
@param storyboardName storyboardName
@param imageNames 图片名字数组
@return LaunchIntroductionView对象
*/
+ (instancetype)sharedWithStoryboardName:(NSString *)storyboardName images:(NSArray *)imageNames;
/**
用storyboard创建的project调用此方法
@param storyboardName storyboardName
@param imageNames 图片名字数组
@param buttonImageName 按钮图片名字
@param frame 按钮的frame
@return LaunchIntroductionView对象
*/
+(instancetype)sharedWithStoryboard:(NSString *)storyboardName images:(NSArray *) imageNames buttonImage:(NSString *) buttonImageName buttonFrame:(CGRect ) frame;
@end
.m文件实现:
#import "LaunchIntroductionView.h"
static NSString *const kAppVersion = @"appVersion";
@interface LaunchIntroductionView ()<UIScrollViewDelegate>
{
UIScrollView *launchScrollView;
UIPageControl *page;
}
@end
@implementation LaunchIntroductionView
NSArray *images;
BOOL isScrollOut;//在最后一页再次滑动是否隐藏引导页
CGRect enterBtnFrame;
NSString *enterBtnImage;
static LaunchIntroductionView *launch = nil;
NSString *storyboard;
#pragma mark - 创建对象-->>不带button
+(instancetype)sharedWithImages:(NSArray *)imageNames{
images = imageNames;
isScrollOut = YES;
launch = [[LaunchIntroductionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height)];
launch.backgroundColor = [UIColor whiteColor];
return launch;
}
#pragma mark - 创建对象-->>带button
+(instancetype)sharedWithImages:(NSArray *)imageNames buttonImage:(NSString *)buttonImageName buttonFrame:(CGRect)frame{
images = imageNames;
isScrollOut = NO;
enterBtnFrame = frame;
enterBtnImage = buttonImageName;
launch = [[LaunchIntroductionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height)];
launch.backgroundColor = [UIColor whiteColor];
return launch;
}
#pragma mark - 用storyboard创建的项目时调用,不带button
+ (instancetype)sharedWithStoryboardName:(NSString *)storyboardName images:(NSArray *)imageNames {
images = imageNames;
storyboard = storyboardName;
isScrollOut = YES;
launch = [[LaunchIntroductionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height)];
launch.backgroundColor = [UIColor whiteColor];
return launch;
}
#pragma mark - 用storyboard创建的项目时调用,带button
+ (instancetype)sharedWithStoryboard:(NSString *)storyboardName images:(NSArray *)imageNames buttonImage:(NSString *)buttonImageName buttonFrame:(CGRect)frame{
images = imageNames;
isScrollOut = NO;
enterBtnFrame = frame;
storyboard = storyboardName;
enterBtnImage = buttonImageName;
launch = [[LaunchIntroductionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height)];
launch.backgroundColor = [UIColor whiteColor];
return launch;
}
#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addObserver:self forKeyPath:@"currentColor" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"nomalColor" options:NSKeyValueObservingOptionNew context:nil];
if ([self isFirstLauch]) {
UIStoryboard *story;
if (storyboard) {
story = [UIStoryboard storyboardWithName:storyboard bundle:nil];
}
UIWindow *window = [UIApplication sharedApplication].windows.lastObject;
if (story) {
UIViewController * vc = story.instantiateInitialViewController;
window.rootViewController = vc;
[vc.view addSubview:self];
}else {
[window addSubview:self];
}
[self addImages];
}else{
[self removeFromSuperview];
}
}
return self;
}
#pragma mark - 判断是不是首次登录或者版本更新
-(BOOL )isFirstLauch{
//获取当前版本号
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *currentAppVersion = infoDic[@"CFBundleShortVersionString"];
//获取上次启动应用保存的appVersion
NSString *version = [[NSUserDefaults standardUserDefaults] objectForKey:kAppVersion];
//版本升级或首次登录
if (version == nil || ![version isEqualToString:currentAppVersion]) {
[[NSUserDefaults standardUserDefaults] setObject:currentAppVersion forKey:kAppVersion];
[[NSUserDefaults standardUserDefaults] synchronize];
return YES;
}else{
return NO;
}
}
#pragma mark - 添加引导页图片
-(void)addImages{
[self createScrollView];
}
#pragma mark - 创建滚动视图
-(void)createScrollView{
launchScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height)];
launchScrollView.showsHorizontalScrollIndicator = NO;
launchScrollView.bounces = NO;
launchScrollView.pagingEnabled = YES;
launchScrollView.delegate = self;
launchScrollView.contentSize = CGSizeMake(kScreen_width * images.count, kScreen_height);
[self addSubview:launchScrollView];
for (int i = 0; i < images.count; i ++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * kScreen_width, 0, kScreen_width, kScreen_height)];
imageView.image = [UIImage imageNamed:images[i]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[launchScrollView addSubview:imageView];
if (i == images.count - 1) {
//判断要不要添加button
if (!isScrollOut) {
UIButton *enterButton = [[UIButton alloc] initWithFrame:CGRectMake(enterBtnFrame.origin.x, enterBtnFrame.origin.y, enterBtnFrame.size.width, enterBtnFrame.size.height)];
enterButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
[enterButton setImage:[UIImage imageNamed:enterBtnImage] forState:UIControlStateNormal];
[enterButton addTarget:self action:@selector(enterBtnClick) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:enterButton];
imageView.userInteractionEnabled = YES;
}
}
}
page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kScreen_height*0.9, kScreen_width, 30)];
page.numberOfPages = images.count;
page.backgroundColor = [UIColor clearColor];//7.13原来颜色
page.currentPageIndicatorTintColor = [UIColor redColor];//7.13tianjia
page.pageIndicatorTintColor = [UIColor grayColor];//7.13tianjia
page.currentPage = 0;
page.defersCurrentPageDisplay = YES;
[self addSubview:page];
}
#pragma mark - 进入按钮
-(void)enterBtnClick{
[self hideGuidView];
}
#pragma mark - 隐藏引导页
-(void)hideGuidView{
[UIView animateWithDuration:0.5 animations:^{
self.alpha = 0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self removeFromSuperview];
});
}];
}
#pragma mark - scrollView Delegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
int cuttentIndex = (int)(scrollView.contentOffset.x + kScreen_width/2)/kScreen_width;
if (cuttentIndex == images.count - 1) {
if ([self isScrolltoLeft:scrollView]) {
if (!isScrollOut) {
return ;
}
[self hideGuidView];
}
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView == launchScrollView) {
int cuttentIndex = (int)(scrollView.contentOffset.x + kScreen_width/2)/kScreen_width;
page.currentPage = cuttentIndex;
}
}
#pragma mark - 判断滚动方向
-(BOOL )isScrolltoLeft:(UIScrollView *) scrollView{
//返回YES为向左反动,NO为右滚动
if ([scrollView.panGestureRecognizer translationInView:scrollView.superview].x < 0) {
return YES;
}else{
return NO;
}
}
#pragma mark - KVO监测值的变化
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"currentColor"]) {
page.currentPageIndicatorTintColor = self.currentColor;
}
if ([keyPath isEqualToString:@"nomalColor"]) {
page.pageIndicatorTintColor = self.nomalColor;
}
}
@end