前言:当我们第一次安装App或者app更新之后,打开时,都会出现一个新手引导页,而当我们第二次打开的时候,引导页就没有了,会直接进入到主界面。那是怎么做的呢?这就让我们细细的品一下吧!
先欣赏一下特效:
再细细的分析一下:
据说思路是这样的:
我们要达成这样的效果,需要根据判断当前应用的版本号与沙盒存储的版本号是否一致,来决定是否展示新特性界面。如果一致,就说明我们之前已经加载过新特性了,我们就会把主界面的控制器做为根视图控制器,就会直接进入主界面。如果不一致,那么我们就传入一个含有图片名称的数组,和一个视图控制器,并返回一个新特性的控制器,把它作为根控制器进行新特性界面的加载。
如下图:
业务逻辑
代码走向图:
- ** 由于,这张图挺大的,有点不清晰,你可以去下载Demo,里面有!**
代码示例:
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
#import "ZYGuideViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
>
BOOL shouldShow = [ZYGuideViewController zy_shouldShowNewFeature];
shouldShow = YES; // Set YES for test
if (shouldShow) {
NSArray *imageNames = @[@"newfeature1.jpg", @"newfeature2.jpg", @"newfeature3.jpg", @"newfeature4.jpg"];
>
ZYGuideViewController *guideVC = [ZYGuideViewController zy_newFeatureWithImageNames:imageNames rootViewController:[[ViewController alloc]init]];
guideVC.hideSkipButton = NO; // show skip Button
self.window.rootViewController = guideVC;
}else{
self.window.rootViewController = [[ViewController alloc]init];
}
>
[self.window makeKeyAndVisible];
>
return YES;
}
ZYGuideViewController.h
#import <UIKit/UIKit.h>
>
@interface ZYGuideViewController : UIViewController
>
#pragma mark - Properties
/**
Whether to hide pageControl, default is NO which means show pageControl.
*/
@property (nonatomic, assign) BOOL hidePageControl;
/**
Whether to hide skip Button, default is YES which means hide skip Button.
*/
@property (nonatomic, assign) BOOL hideSkipButton;
/**
Current page indicator tint color, default is [UIColor whiteColor].
*/
@property (nonatomic, strong) UIColor *currentPageIndicatorTintColor;
/**
Other page indicator tint color, default is [UIColor lightGrayColor].
*/
@property (nonatomic, strong) UIColor *pageIndicatorTintColor;
#pragma mark - Class methods
/**
Only the first start app need show new features.
@return return YES to show, NO not to show.
*/
+ (BOOL)zy_shouldShowNewFeature;
/**
Create new features view controller with images.
@param imageNames The image's name array.
@param rootVC The key window's true root view controller.
@return SRNewFeatureViewController
*/
+ (instancetype)zy_newFeatureWithImageNames:(NSArray *)imageNames rootViewController:(UIViewController *)rootVC;
@end
ZYGuideViewController.m
#import "ZYGuideViewController.h"
@interface ZYGuideViewController () <UIScrollViewDelegate>
@end
@implementation ZYGuideViewController
{
UIViewController *_rootVC;
NSArray *_imageNames;
UIPageControl *_pageControl;
UIButton *_skipButton;
}
+ (BOOL)zy_shouldShowNewFeature
{
// the app version in the sandbox
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] stringForKey:@"CFBundleShortVersionString"];
// the current app version
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
// Compare versions
if ([currentVersion isEqualToString:lastVersion]) {
return NO;
}else {
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"CFBundleShortVersionString"];
[[NSUserDefaults standardUserDefaults] synchronize];
return YES;
}
>
}
- (instancetype)initWithImageNames:(NSArray *)imageNames rootViewController:(UIViewController *)rootVC
{
if (self = [super init]) {
self.view.backgroundColor = [UIColor whiteColor];
_imageNames = imageNames;
_rootVC = rootVC;
[self setup];
}
return self;
}
+ (instancetype)zy_newFeatureWithImageNames:(NSArray *)imageNames rootViewController:(UIViewController *)rootVC
{
return [[self alloc]initWithImageNames:imageNames rootViewController:rootVC];
}
- (void)setup {
>
if(_imageNames.count > 0){
UIScrollView *scrollView = [[UIScrollView alloc]init];
[self.view addSubview:({
scrollView.delegate = self;
scrollView.bounces = NO;
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.frame = self.view.bounds;
scrollView.contentSize = (CGSize){self.view.frame.size.width * _imageNames.count, 0};
scrollView;
})];
>
CGFloat imageW = self.view.frame.size.width;
CGFloat imageH = self.view.frame.size.height;
>
for (int i = 0; i < _imageNames.count; i++) {
>
[scrollView addSubview:({
UIImageView *imageView = [[UIImageView alloc] init];
>
[imageView setImage:[UIImage imageNamed:_imageNames[i]]];
[imageView setFrame:(CGRect){imageW * i, 0, imageW, imageH}];
if (i == _imageNames.count - 1) {
// Here you can add a custom Button to switch the root controller.
// Such as
{
UIButton *customBtn = [[UIButton alloc] init];
[customBtn setImage:[UIImage imageNamed:@"start_huanshi_AR_normal"] forState:UIControlStateNormal];
[customBtn setImage:[UIImage imageNamed:@"start_huanshi_AR_pressed"] forState:UIControlStateHighlighted];
[customBtn sizeToFit];
customBtn.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height - customBtn.frame.size.height - 100);
[customBtn addTarget:self action:@selector(tapAciton) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:customBtn];
[imageView setUserInteractionEnabled:YES];
}
[imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAciton)];
[imageView addGestureRecognizer:tap];
}
imageView;
>
})];
}
>
[self.view addSubview:({
_pageControl = [[UIPageControl alloc] init];
[_pageControl setNumberOfPages:_imageNames.count];
[_pageControl setHidesForSinglePage:YES];
[_pageControl setUserInteractionEnabled:NO];
[_pageControl setCurrentPageIndicatorTintColor:[UIColor whiteColor]];
[_pageControl setPageIndicatorTintColor:[UIColor lightGrayColor]];
[_pageControl setFrame:CGRectMake(0, self.view.frame.size.height * 0.9, self.view.frame.size.width, 40)];
_pageControl;
})];
[self.view addSubview:({
CGFloat margin = 10;
_skipButton = [[UIButton alloc] init];
_skipButton.frame = CGRectMake(self.view.frame.size.width - margin - 60, self.view.frame.size.height - margin - 40, 60, 30);
_skipButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
_skipButton.layer.cornerRadius = 15;
_skipButton.layer.masksToBounds = YES;
_skipButton.titleLabel.adjustsFontSizeToFitWidth = YES;
_skipButton.titleLabel.font = [UIFont systemFontOfSize:16];
_skipButton.hidden = YES;
[_skipButton setTitle:@"跳过" forState:UIControlStateNormal];
[_skipButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[_skipButton addTarget:self action:@selector(skipBtnAction) forControlEvents:UIControlEventTouchUpInside];
_skipButton;
})];
}}
- (void)tapAciton {
[UIView transitionWithView:[UIApplication sharedApplication].keyWindow
duration:0.75f
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[UIApplication sharedApplication].keyWindow.rootViewController = _rootVC;
}
completion:nil];
}
- (void)skipBtnAction {
>
[self tapAciton];
}
#pragma mark - Setters
- (void)setCurrentPageIndicatorTintColor:(UIColor *)currentPageIndicatorTintColor {
>
_currentPageIndicatorTintColor = currentPageIndicatorTintColor;
>
[_pageControl setCurrentPageIndicatorTintColor:currentPageIndicatorTintColor];
}
- (void)setPageIndicatorTintColor:(UIColor *)pageIndicatorTintColor {
>
_pageIndicatorTintColor = pageIndicatorTintColor;
>
[_pageControl setPageIndicatorTintColor:pageIndicatorTintColor];
}
- (void)setHidePageControl:(BOOL)hidePageControl {
>
_hidePageControl = hidePageControl;
>
_pageControl.hidden = hidePageControl;
}
- (void)setHideSkipButton:(BOOL)hideSkipButton {
>
_hideSkipButton = hideSkipButton;
>
_skipButton.hidden = hideSkipButton;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
>
NSInteger page = scrollView.contentOffset.x / scrollView.bounds.size.width;
_pageControl.currentPage = page;
}
@end
demo下载地址
若果您觉得不错,请滑动一下手指,点个心,以资鼓励!若果,你觉得,太辛苦了,也可以稍微打赏一下哦!!(__),互相交流!!!**