UICollectionViewController 版本新特性

AppDelegate.m 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    NSString *curVersion =  [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    
    NSString *lastVersion =  [[NSUserDefaults standardUserDefaults]objectForKey:@"vision"];
    
    NSLog(@"%@",[NSUserDefaults standardUserDefaults]);
    
    UIViewController *rootVC;
    
    
    if ([curVersion isEqualToString:lastVersion]) {
        rootVC = [[TabBarController alloc]init];
    }else{
        rootVC = [[ZCCollectionViewController alloc]init];
        [[NSUserDefaults standardUserDefaults]setObject:curVersion forKey:@"vision"];
    }
    
    self.window.rootViewController =rootVC;
    
    [self.window makeKeyAndVisible];
    return YES;
}

UICollectionViewController

//
//  ZCCollectionViewController.m
//  UICollectionDemo
//
//  Created by iOS on 13/9/30.
//  Copyright © 2015年 iOS. All rights reserved.
//

#import "ZCCollectionViewController.h"
#import "ZCCollectionViewCell.h"
#import "UIView+Frame.h"
@interface ZCCollectionViewController ()
@property(nonatomic,weak)UIPageControl *pageControl;
@end

@implementation ZCCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    // 设置cell的尺寸
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    return [super initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
    
    [self.collectionView registerClass:[ZCCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self setPageControl];
}

- (void)setUp{
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.pagingEnabled = YES;
}

- (void)setPageControl{
    UIPageControl *control = [[UIPageControl alloc]init];
    control.numberOfPages = 4;
    control.pageIndicatorTintColor = [UIColor blackColor];
    control.currentPageIndicatorTintColor = [UIColor redColor];
    control.center = CGPointMake(self.view.centerX, self.view.centerY);
    [self.view addSubview:control];
    _pageControl = control;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int page = scrollView.contentOffset.x/scrollView.bounds.size.width+0.5;
    _pageControl.currentPage =page;
}


#pragma mark <UICollectionViewDataSource>

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


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZCCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
//     CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    
    NSString *imageName = [NSString stringWithFormat:@"new_feature_%ld",indexPath.row + 1];
//    if (screenH > 480) { // 5 , 6 , 6 plus
//        imageName = [NSString stringWithFormat:@"new_feature_%ld-568h",indexPath.row + 1];
//    }
    cell.image = [UIImage imageNamed:imageName];
    
    [cell setIndexPath:indexPath count:4];
    
    return cell;
}


@end

UICollectionViewCell.h

//
//  ZCCollectionViewCell.h
//  UICollectionDemo
//
//  Created by iOS on 13/9/30.
//  Copyright © 2015年 iOS. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ZCCollectionViewCell : UICollectionViewCell
@property(nonatomic,weak)UIImage *image;

- (void)setIndexPath:(NSIndexPath *)indexPath count:(int)count;
@end
UICollectionViewCell.m
//
//  ZCCollectionViewCell.m
//  UICollectionDemo
//
//  Created by iOS on 13/9/30.
//  Copyright © 2015年 iOS. All rights reserved.
//

#import "ZCCollectionViewCell.h"
#import "UIView+Frame.h"
#import "TabBarController.h"
@interface ZCCollectionViewCell()
@property (nonatomic, weak) UIImageView *imageView;

@property (nonatomic, weak) UIButton *shareButton;

@property (nonatomic, weak) UIButton *startButton;
@end

@implementation ZCCollectionViewCell


- (UIButton *)shareButton{
    if (_shareButton==nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"分享给大家" forState:UIControlStateNormal];
        [btn setTintColor:[UIColor orangeColor]];
        [btn sizeToFit];
        
        [self.contentView addSubview:btn];
        
        _shareButton = btn;
    }
    return _shareButton;
}

- (UIButton *)startButton{
    if (_startButton==nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"开始" forState:UIControlStateNormal];
        [btn setTintColor:[UIColor orangeColor]];
        [btn sizeToFit];
        [btn addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:btn];
        _startButton =btn;
    }
    return _startButton;
}

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

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

- (void)layoutSubviews{
    [super layoutSubviews];
    self.imageView.frame =self.bounds;
    
    self.shareButton.center = CGPointMake(self.width*0.5, self.height*0.8);
    self.startButton.center = CGPointMake(self.width*0.5, self.height*0.9);
}

- (void)setIndexPath:(NSIndexPath *)indexPath count:(int)count{
    if (indexPath.row==count-1) {
        self.shareButton.hidden=NO;
        self.startButton.hidden=NO;
    }else{
        self.shareButton.hidden=YES;
        self.startButton.hidden= YES;
    }
}

- (void)start{
    TabBarController *tab = [[TabBarController alloc]init];
    
    
    UIWindow *kwindow =  [UIApplication sharedApplication].keyWindow;
    
    kwindow.rootViewController = tab;
}

@end



你可能感兴趣的:(UICollectionViewController 版本新特性)