iOS-Main-新特性轮播引导图的精装详解

前言:当我们第一次安装App或者app更新之后,打开时,都会出现一个新手引导页,而当我们第二次打开的时候,引导页就没有了,会直接进入到主界面。那是怎么做的呢?这就让我们细细的品一下吧!

先欣赏一下特效:

引导图.gif

再细细的分析一下:
据说思路是这样的:
我们要达成这样的效果,需要根据判断当前应用的版本号与沙盒存储的版本号是否一致,来决定是否展示新特性界面。如果一致,就说明我们之前已经加载过新特性了,我们就会把主界面的控制器做为根视图控制器,就会直接进入主界面。如果不一致,那么我们就传入一个含有图片名称的数组,和一个视图控制器,并返回一个新特性的控制器,把它作为根控制器进行新特性界面的加载。
如下图:


业务逻辑


代码走向图:
封装引导图.png

  • ** 由于,这张图挺大的,有点不清晰,你可以去下载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下载地址
若果您觉得不错,请滑动一下手指,点个心,以资鼓励!若果,你觉得,太辛苦了,也可以稍微打赏一下哦!!(__),互相交流!!!**

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,948评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,371评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,490评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,521评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,627评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,842评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,997评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,741评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,203评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,534评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,673评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,339评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,955评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,770评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,000评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,394评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,562评论 2 349

推荐阅读更多精彩内容