利用UICollectionView实现"新特性"功能demo

1.手动创建窗口,启动。判断是否有新版本,展示新特性界面
//
//  AppDelegate.m


#import "AppDelegate.h"
#import "NewFeatureViewController.h"

#define VersionKey @"version"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    // 创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor redColor];
    
    // 判断有木有最新的版本号
    // 从info.plist获取软件版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleInfoDictionaryVersion"];
    NSString *oldVersion = [[NSUserDefaults standardUserDefaults] objectForKey:VersionKey];
    
    UIViewController *rootVC = nil;
    if ([currentVersion isEqualToString:oldVersion]) {
        // 没有新版本
        rootVC = [[UITabBarController alloc] init];
    }else{
         rootVC = [[NewFeatureViewController alloc] init];
        
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:VersionKey];
    }
    
    // 设置窗口的根控制器
    self.window.rootViewController = rootVC;
    // 显示窗口
    [self.window makeKeyAndVisible];
    
    
    return YES;
}


@end

2.新特性

//
//  NewFeatureViewController.h

#import <UIKit/UIKit.h>

@interface NewFeatureViewController : UICollectionViewController

@end
//
//  NewFeatureViewController.m

#import "NewFeatureViewController.h"
#import "NewFeatureCell.h"

#define ScreenBounds [UIScreen mainScreen].bounds
@interface NewFeatureViewController ()

@end

@implementation NewFeatureViewController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init
{
    // 流水布局对象
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置cell尺寸
    layout.itemSize = CGSizeMake(ScreenBounds.size.width, ScreenBounds.size.height);
    // cell间距
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    // 滚动的方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    return [super initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // 弹簧效果
    self.collectionView.bounces = NO;
    // 滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    // 分页
    self.collectionView.pagingEnabled = YES;
    
    
    // 注册cell
    [self.collectionView registerClass:[NewFeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];
}

#pragma mark <UICollectionViewDataSource>

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

// 返回第section组有多少个cell
#define Pages 4
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return Pages;
}

/**
 *  返回cell
 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.image = [UIImage imageNamed:[NSString stringWithFormat:@"guide%ldBackground",indexPath.item + 1]];
    
    // 告诉cell什么时候是最后一个
    [cell setupIndexPath:indexPath count:Pages];
    
    return cell;
}

@end
//
//  NewFeatureCell.h


#import <UIKit/UIKit.h>

@interface NewFeatureCell : UICollectionViewCell

@property(nonatomic,strong)UIImage *image;

- (void)setupIndexPath:(NSIndexPath *)indexPath count:(NSInteger)pagesCount;

@end
//
//  NewFeatureCell.m

#import "NewFeatureCell.h"

#define ScreenBounds [UIScreen mainScreen].bounds

@interface NewFeatureCell()

@property(nonatomic,weak)UIImageView *imageView;
@property(nonatomic,weak)UIButton *startButton;

@end

@implementation NewFeatureCell

- (UIButton *)startButton
{
    if (_startButton == nil) {
        
        UIButton *btn = [[UIButton alloc] init];
        [btn setBackgroundImage:[UIImage imageNamed:@"guideStart"] forState:UIControlStateNormal];
        [btn sizeToFit];
        btn.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.9);
        [btn addTarget:self action:@selector(startButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:btn];
       
        _startButton = btn;
    }
    return _startButton;
}

/**
 *  点击了startButton按钮
 */
- (void)startButtonClicked
{
    // 切换根控制器
    self.window.rootViewController = [[UITabBarController alloc] init];
}


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

- (void)setImage:(UIImage *)image
{
    _image = image;
    
    self.imageView.image = image;
}

- (void)setupIndexPath:(NSIndexPath *)indexPath count:(NSInteger)pagesCount
{
    if (indexPath.row == pagesCount - 1) {
        // 最后一个cell
        // 添加跳转按钮
        self.startButton.hidden = NO;
    }else{
        self.startButton.hidden = YES;
    }
}

@end





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值