iOS学习笔记-096.彩票11——新特性

彩票11——新特性

一、图示

这里写图片描述

二、分析

2.1 什么时候出现新特性界面呢?

当有版本更新或者第一次安装的时候出现新特性界面。

2.2 如何判断是第一次或者有版本更新呢?

我们本地保存上一次的版本号。然后获取当前的版本好,如果不相同进入新特新界面,如果相同进入主界面

2.3 新特性界面用什么实现呢?

可以使用 UICollectionView 实现


三、新特性界面

3.1 说明

我们创建一个 QWMNewFeatureCollectionViewController 控制器,继承自 UICollectionViewController

我们创建一个流动不布局,去除我们不需要的效果。我们还需要创建一个我们自己cell

3.2 QWMNewFeatureCollectionViewCell

QWMNewFeatureCollectionViewCell.h

//
//  QWMNewFeatureCollectionViewCell.h
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/8/27.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import <UIKit/UIKit.h>
@interface QWMNewFeatureCollectionViewCell : UICollectionViewCell
/** 背景图片 */
@property (nonatomic, strong) UIImage *image;

- (void)setIndexPath:(NSIndexPath *)indexPath count:(NSInteger)count;

@end

QWMNewFeatureCollectionViewCell.m

//
//  QWMNewFeatureCollectionViewCell.m
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/8/27.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import "QWMNewFeatureCollectionViewCell.h"
#import "QWMTabBarViewController.h"

@interface QWMNewFeatureCollectionViewCell()

/** 背景图片 */
@property (nonatomic, weak) UIImageView *bgImageView;
/** 立即体验按钮 */
@property (nonatomic, weak) UIButton *startBtn;

@end

@implementation QWMNewFeatureCollectionViewCell

-(UIImageView *)bgImageView{
    if(!_bgImageView){
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.bounds];
        [self.contentView addSubview:imageView];
        _bgImageView = imageView;
    }
    return _bgImageView;
}

-(void)setImage:(UIImage *)image{
    _image = image;
    [[self bgImageView] setImage:image];
}


-(UIButton *)startBtn{
    if(!_startBtn){
        UIButton *button = [[UIButton alloc]init];
        //设置背景图片
        [button setBackgroundImage:[UIImage imageNamed:@"guideStart"] forState:UIControlStateNormal];
        [button sizeToFit];
        //位置
        button.center = CGPointMake(self.width/2, self.height*0.9f);
        [self addSubview:button];
        [button addTarget:self action:@selector(buttonOnClick:) forControlEvents:UIControlEventTouchUpInside ];
        _startBtn = button;
    }
    return _startBtn;
}

// 点击立即体验按钮的时候就会调用
- (void)buttonOnClick:(UIButton *)button{
    // 切换窗口的跟控制器
    QWMTabBarViewController *tabBarVC = [[QWMTabBarViewController alloc] init];

    QWMKeyWindow.rootViewController =  tabBarVC;

}

-(void)setIndexPath:(NSIndexPath *)indexPath count:(NSInteger)count{
    self.startBtn.hidden = indexPath.item != count-1;
}

@end

3.3 QWMNewFeatureCollectionViewController

//
//  QWMNewFeatureCollectionViewController.m
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/8/27.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import "QWMNewFeatureCollectionViewController.h"
#import "QWMNewFeatureCollectionViewCell.h"
@interface QWMNewFeatureCollectionViewController ()

/** 记录上一次偏移量 */
@property (nonatomic, assign) CGFloat lastOffsetX;

@property (nonatomic, weak) UIImageView *guide;

@property (nonatomic, weak) UIImageView *guideLargeText;

@property (nonatomic, weak) UIImageView *guideSmallText;

@end

@implementation QWMNewFeatureCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //修改item的大小
    flowLayout.itemSize = [UIScreen mainScreen].bounds.size;

    //设置行间距
    flowLayout.minimumLineSpacing = 0;

    // 修改每一个item的间距
    flowLayout.minimumInteritemSpacing = 0;

    //修改为水平方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    return [super initWithCollectionViewLayout:flowLayout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //注册QWMNewFeatureCollectionViewCell
    [self.collectionView registerClass:[QWMNewFeatureCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

    //去除弹簧效果
    self.collectionView.bounces =NO;
    //去除滑动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    //设置分页 不设置这个,我们滑动的时候,有可能会滑动多个
    self.collectionView.pagingEnabled = YES;

     [self setupAddChildImageView];
}

-(void)setupAddChildImageView{
    //1.线
    UIImageView *guideLine = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideLine"]];
    guideLine.x -=150;
    [self.collectionView addSubview:guideLine];

    //2.导航图片
    UIImageView *guide = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide1"]];
    guide.x += 50;
    [self.collectionView addSubview:guide];
    self.guide = guide;

    //3.大标题
    UIImageView *guideLargeText = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    guideLargeText.center = CGPointMake(self.view.width/2, self.view.height*0.7f);
    [self.collectionView addSubview:guideLargeText];
    self.guideLargeText = guideLargeText;

    //4.小标题
    UIImageView *guideSmallText = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    guideSmallText.center = CGPointMake(self.view.width/2, self.view.height * 0.8f);
    [self.collectionView addSubview:guideSmallText];
    self.guideSmallText = guideSmallText;

}

//滑动减速的时候调用
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //总偏移量
    CGFloat offsetX = scrollView.contentOffset.x;

    //单个偏移量
    CGFloat del = offsetX - self.lastOffsetX;

    //页码
    NSInteger page = offsetX/del;

    //设置图片
    NSString *guideName = [NSString stringWithFormat:@"guide%ld",page+1];
    NSString *largerName = [NSString stringWithFormat:@"guideLargeText%ld",page+1];
    NSString *smallName = [NSString stringWithFormat:@"guideSmallText%ld",page+1];
    self.guide.image = [UIImage imageNamed:guideName];
    self.guideLargeText.image = [UIImage imageNamed:largerName];
    self.guideSmallText.image = [UIImage imageNamed:smallName];

    //设置偏移 这么设置是为了 拥有滑入的动画
    self.guide.x += del * 2;
    self.guideLargeText.x += del * 2;
    self.guideSmallText.x += del * 2;

    [UIView animateWithDuration:0.1 animations:^{
        self.guide.x -= del;
        self.guideLargeText.x -= del;
        self.guideSmallText.x -= del;
    }];

    self.lastOffsetX = offsetX;
}

#define QWMPage 4
//每组的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return QWMPage;
}

//组数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

// 返回条目
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    QWMNewFeatureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    //创建背景图片
    NSString *name = [NSString stringWithFormat:@"guide%ldBackground",indexPath.item+1];
    UIImage *image = [UIImage imageNamed:name];
    cell.image = image;
    [cell setIndexPath:indexPath count:QWMPage];
    return cell;
}



@end

四、保存工具

4.1 QWMSaveTool.h

//
//  QWMSaveTool.h
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/8/27.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface QWMSaveTool : NSObject
+ (nullable id)objectForKey:(NSString *)defaultName;

+ (void)setObject:(nullable id)value forKey:(NSString *)defaultName;
@end

4.2 QWMSaveTool.m

//
//  QWMSaveTool.m
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/8/27.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import "QWMSaveTool.h"

@implementation QWMSaveTool
+ (nullable id)objectForKey:(NSString *)defaultName{
   return [[NSUserDefaults standardUserDefaults] objectForKey:defaultName];
}

+ (void)setObject:(nullable id)value forKey:(NSString *)defaultName{
    if(defaultName){
        [[NSUserDefaults standardUserDefaults] setObject:value forKey:defaultName];
    }
}
@end

五、获取根控制器工具

5.1 QWMRootVC.h

//
//  QWMRootVC.h
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/8/27.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface QWMRootVC : NSObject
+ (UIViewController *)chooseWindowRootVC;
@end

5.2 QWMRootVC.m

//
//  QWMRootVC.m
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/8/27.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import "QWMRootVC.h"
#import "QWMSaveTool.h"
#import "QWMNewFeatureCollectionViewController.h"
#import "QWMTabBarViewController.h"

#define QWMVersion @"version"

@implementation QWMRootVC

+(UIViewController *)chooseWindowRootVC{
    //1.获取上一次的版本
    NSString *lastVersion = [QWMSaveTool objectForKey:QWMVersion];

    //2.获取当前版本
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];

    //3.判断我们返回那个控制器
    UIViewController *rootVc;
    if(![lastVersion isEqualToString:currentVersion]){
        //新特性
        rootVc = [[QWMNewFeatureCollectionViewController alloc]init];
        //保存版本
        [QWMSaveTool setObject:QWMVersion forKey:currentVersion];
    }else{
        //主界面
        rootVc = [[QWMTabBarViewController alloc]init];
    }
    return rootVc;
}

@end

六、AppDelegate修改

//
//  AppDelegate.m
//  03_UIView79_彩票
//
//  Created by 杞文明 on 17/7/24.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import "AppDelegate.h"
#import "QWMTabBarViewController.h"
#import "QWMNewFeatureCollectionViewController.h"
#import "QWMRootVC.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    //1.创建窗口
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    //2.设置窗口根控制器
    self.window.rootViewController = [QWMRootVC chooseWindowRootVC];

    //3.让窗口显示
    [self.window makeKeyWindow];

    return YES;
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值