.h
#import <UIKit/UIKit.h>
typedef void(^GuideBlock)();
typedef void(^LaunchBlock)();
@interface LXGuideAndLaunch : UIViewController<UIScrollViewDelegate>
@property(nonatomic,strong)UIView *guideView;//引导页
@property(nonatomic,strong)UIView *launchView;//发起页
@property(nonatomic,strong)NSArray *pictures;//引导页的图片数组
@property(nonatomic,copy)GuideBlock guideBlock;
@property(nonatomic,copy)LaunchBlock launchBlock;
@property(nonatomic,strong)UIViewController *mainViewController;//程序主控制器
-(void)storeFirstName:(NSString *)notFirst andMainViewController:(UIViewController *)mainVC guide:(GuideBlock)guide launch:(LaunchBlock)launch;//notFirst是作为存贮的key,mainVC进入程序之后的主控制器
-(void)scrollGuideWithPicturesName:(NSArray *)pictures progressName:(NSArray *)progresses;//滑动视图样式
-(void)viewLaunchAdvertisePatternWithImage:(NSString *)imageName time:(NSInteger)time;//广告样式的发起页配置
@end
.m
#import "LXGuideAndLaunch.h"
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
@interface LXGuideAndLaunch ()
@end
@implementation LXGuideAndLaunch
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)storeFirstName:(NSString *)notFirst andMainViewController:(UIViewController *)mainVC guide:(GuideBlock)guide launch:(LaunchBlock)launch;{
self.mainViewController=mainVC;
self.guideBlock=guide;
self.launchBlock=launch;
BOOL isFirst=[[NSUserDefaults standardUserDefaults]boolForKey:notFirst];
if (isFirst==NO) {
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:notFirst];
[[NSUserDefaults standardUserDefaults]synchronize];
[self createGuide];
}else{
self.launchView=[[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:self.launchView];
[self createLaunch];
}
}
-(void)createGuide{//引导页
self.guideView=[[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:self.guideView];
self.guideBlock();
}
-(void)scrollGuideWithPicturesName:(NSArray *)pictures progressName:(NSArray *)progresses{
self.pictures=pictures;
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
scrollView.pagingEnabled=YES;//是否分页效果
scrollView.showsHorizontalScrollIndicator=NO;//是否显示水平滚动条
// scrollView.autoresizesSubviews=NO;
scrollView.delegate=self;
scrollView.contentSize=CGSizeMake(pictures.count*kWidth, kHeight);
for (NSInteger i=0; i<pictures.count; i++) {
NSString *picture=pictures[i];//引导图片的名字
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * i, 0, kScreenWidth, kScreenHeight)];//引导图
imageView.image=[UIImage imageNamed:picture];
NSString *progress=progresses[i];//进度条图片的名字
UIImageView *progressView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 27, kScreenWidth, 27)];//进度条
progressView.image = [UIImage imageNamed:progress];
progressView.contentMode = UIViewContentModeCenter;
[imageView addSubview:progressView];
[scrollView addSubview:imageView];
}
[self.guideView addSubview:scrollView];
if (progresses.count==0) {
//创建UIPageControl
UIPageControl *pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(kWidth/3, kHeight-50, kWidth/3, 44)];
pageControl.numberOfPages=pictures.count;
pageControl.tag=1002;
[self.guideView insertSubview:pageControl aboveSubview:scrollView];
}
}
-(void)createLaunch{//发起页
self.launchView=[[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:self.launchView];
self.launchBlock();
}
-(void)viewLaunchAdvertisePatternWithImage:(NSString *)imageName time:(NSInteger)time;{
UIImageView *view=[[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
view.image=[UIImage imageNamed:imageName];
[self.launchView addSubview:view];
UILabel *timeLabel=[[UILabel alloc]initWithFrame:CGRectMake(kWidth-40, 20, 30, 30)];
[self.launchView insertSubview:timeLabel aboveSubview:view];
timeLabel.text=[NSString stringWithFormat:@"%ld",time];
timeLabel.tag=1001;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction:) userInfo:@(time) repeats:YES];
}
-(void)timeAction:(NSTimer *)timer{
static NSInteger time;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
time=[timer.userInfo integerValue];
});
time--;
UILabel *timeLabel=(UILabel *)[self.launchView viewWithTag:1001];
timeLabel.text=[NSString stringWithFormat:@"%ld",time];
if (time<0) {
[UIApplication sharedApplication].keyWindow.rootViewController = self.mainViewController;
[UIView animateWithDuration:0.5 animations:^{
self.mainViewController.view.transform = CGAffineTransformMakeScale(1.2, 1.2);
} completion:^(BOOL finished) {
self.mainViewController.view.transform = CGAffineTransformIdentity;
}];
[timer invalidate];
return;
}
}
#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.x > (kScreenWidth * (self.pictures.count-1))) {
//先给定一个缩小的效果
self.mainViewController.view.transform = CGAffineTransformMakeScale(0.6, 0.6);
[UIView animateWithDuration:.3 animations:^{
//恢复原始的大小
self.mainViewController.view.transform = CGAffineTransformIdentity;
}];
[UIApplication sharedApplication].keyWindow.rootViewController = self.mainViewController;
}
if ([self.guideView viewWithTag:1002]) {
UIPageControl *pageControl=(UIPageControl *)[self.guideView viewWithTag:1002];
pageControl.currentPage=scrollView.contentOffset.x/kWidth;
}
}
@end
样式:AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MainTabBarController *mainTabBar = [sb instantiateInitialViewController];
LXGuideAndLaunch *GL=[[LXGuideAndLaunch alloc]init];
[GL storeFirstName:@"isFirst" andMainViewController:mainTabBar guide:^{
[GL scrollGuideWithPicturesName:@[@"guide1@2x",@"guide2@2x",@"guide3@2x",@"guide4@2x",@"guide5@2x"]
progressName:nil];
} launch:^{
[GL viewLaunchAdvertisePatternWithImage:@"[email protected]" time:3];
}];
self.window.rootViewController=GL;
return YES;
}