#import "NewFeatureController.h" #import "NewFeatureCell.h" @interface NewFeatureController () @property (nonatomic, weak) UIPageControl *control; @end @implementation NewFeatureController static NSString *ID = @"cell"; - (instancetype)init { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; // 设置cell的尺寸 layout.itemSize = [UIScreen mainScreen].bounds.size; // 清空行距 layout.minimumLineSpacing = 0; // 设置滚动的方向 layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; return [super initWithCollectionViewLayout:layout]; } // self.collectionView != self.view // 注意: self.collectionView 是 self.view的子控件 // 使用UICollectionViewController // 1.初始化的时候设置布局参数 // 2.必须collectionView要注册cell // 3.自定义cell - (void)viewDidLoad { [super viewDidLoad]; // 注册cell,默认就会创建这个类型的cell [self.collectionView registerClass:[NewFeatureCell class] forCellWithReuseIdentifier:ID]; // 分页 self.collectionView.pagingEnabled = YES; self.collectionView.bounces = NO; self.collectionView.showsHorizontalScrollIndicator = NO; // 添加pageController [self setUpPageControl]; } // 添加pageController - (void)setUpPageControl { // 添加pageController,只需要设置位置,不需要管理尺寸 UIPageControl *control = [[UIPageControl alloc] init]; control.numberOfPages = 4; control.pageIndicatorTintColor = [UIColor blackColor]; control.currentPageIndicatorTintColor = [UIColor redColor]; // 设置center control.center = CGPointMake(self.view.width * 0.5, self.view.height); _control = control; [self.view addSubview:control]; } #pragma mark - UIScrollView代理 // 只要一滚动就会调用 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 获取当前的偏移量,计算当前第几页 int page = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5; // 设置页数 _control.currentPage = page; } #pragma mark - UICollectionView代理和数据源 // 返回有多少组 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } // 返回第section组有多少个cell - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 4; } // 返回cell长什么样子 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { // dequeueReusableCellWithReuseIdentifier // 1.首先从缓存池里取cell // 2.看下当前是否有注册Cell,如果注册了cell,就会帮你创建cell // 3.没有注册,报错 NewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath]; // 拼接图片名称 3.5 320 480 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; }
// // NewFeatureCell.h // // // Created by apple on 15-3-7. // Copyright (c) 2015年 apple. All rights reserved. // #import <UIKit/UIKit.h> @interface NewFeatureCell : UICollectionViewCell @property (nonatomic, strong) UIImage *image; // 判断是否是最后一页 - (void)setIndexPath:(NSIndexPath *)indexPath count:(int)count; @end
// NewFeatureCell.m // // // Created by apple on 15-3-7. // Copyright (c) 2015年 apple. All rights reserved. // #import "NewFeatureCell.h" #import "TabBarController.h" @interface NewFeatureCell () @property (nonatomic, weak) UIImageView *imageView; @property (nonatomic, weak) UIButton *shareButton; @property (nonatomic, weak) UIButton *startButton; @end @implementation NewFeatureCell - (UIButton *)shareButton { if (_shareButton == nil) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setTitle:@"分享给大家" forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn sizeToFit]; [self.contentView addSubview:btn]; _shareButton = btn; } return _shareButton; } - (UIButton *)startButton { if (_startButton == nil) { UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [startBtn setTitle:@"开始微博" forState:UIControlStateNormal]; [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal]; [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted]; [startBtn sizeToFit]; [startBtn addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:startBtn]; _startButton = startBtn; } return _startButton; } - (UIImageView *)imageView { if (_imageView == nil) { UIImageView *imageV = [[UIImageView alloc] init]; _imageView = imageV; // 注意:一定要加载contentView [self.contentView addSubview:imageV]; } return _imageView; } // 布局子控件的frame - (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)setImage:(UIImage *)image { _image = image; self.imageView.image = image; } // 判断当前cell是否是最后一页 - (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 { // 进入tabBarVc TabBarController *tabBarVc = [[TabBarController alloc] init]; // 切换根控制器:可以直接把之前的根控制器清空
#define KeyWindow [UIApplication sharedApplication].keyWindow
KeyWindow.rootViewController = tabBarVc; } @end