iOS彩票项目--第四天,新特性界面搭建,UICollectionViewController的初次使用
一、新特性界面搭建的思路:
- 在AppDelegate加载主窗体的时候进行判断程序版本号,直接进入程序或者进入新特性展示界面
- 取出当前的版本号,与旧的版本号相比较(旧的版本号在进入程序的时候存起来 =》建议偏好设置存储)
- 版本号不一样,说明当前版本是新版本需要进入新特性介绍,并将版本号存下来
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 初始化主窗体
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UIViewController *rootVC = nil; // 判断版本号,决定是否进入新特性展示页面
// 取出当前的版本号,与旧的版本号相比较(旧的版本号在真正进入程序的时候存起来 =》建议偏好设置保存)
// 版本号不一样,说明当前的版本是新版本,进入新特性介绍,并保存版本号 // 当前版本号,通过info获得
NSDictionary *dictInfo = [NSBundle mainBundle].infoDictionary;
NSString *curVersion = dictInfo[@"CFBundleShortVersionString"]; // 旧版本号,通过读取偏好设置获得
NSString *oldVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"ChaosVersionKey"]; if ([curVersion isEqualToString:oldVersion]) { // 版本号相等,进入程序
rootVC = [[ChaosTabBarController alloc] init]; rootVC.view.backgroundColor = [UIColor yellowColor];
} else { // 有新版本,进入新特性,保存版本号 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:curVersion forKey:@"ChaosVersionKey"]; // 一般使用流式布局--自己用什么布局,最好在内部设置,进行封装
// UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init]; rootVC = [[ChaosNewFeatureViewController alloc] init]; } self.window.rootViewController = rootVC;
// 显示窗口
[self.window makeKeyAndVisible];
return YES;
}
二、UICollectionViewController 的简单使用
- UICollectionViewController 与 UITableViewController 类似,不同之处在于前者一行可以显示多个cell(可以实现快速九宫格布局),或者一行只能显示一个cell
- UICollectionViewController 的 层次结构需要特别注意一下 首先是控制器的view 控制器view上面是collectionView ==》self.view != self.collectionView;人们看到的都是collectionView
- UICollectionViewController 的 cell 的重复利用必须通过注册[self.collectionView registerClass:[ChaosNewFeatureCell class] forCellWithReuseIdentifier:ID];
- 初始化 UICollectionViewController 必须制定一个布局,建议使用流式布局。通常情况下将 UICollectionViewController 的初始化过程进行封装,因为使用什么布局不需要暴露在外面,代码如下:
- 流式布局的常用属性介绍
三、self.collectionView 的常用属性 -- 继承自UIScrollView,拥有scrollView的所有属性,做项目名的时候,scrollView忘干净了,这里再整理一次
四、数据源方法--与tableView类似
五、新特性界面布局的实现,自定义了cell,cell中只有一个UIImageView属性,为了保证UIImageView只有一个对象,懒加载;还需要一个UIImage让外界来赋值
六、布局完成和动画的代码实现,重点是在这两个方法:
- (void)setUpAllChildrenView
// 滚动减速结束的时候调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
#import "ChaosNewFeatureViewController.h"
#import "ChaosNewFeatureCell.h" @interface ChaosNewFeatureViewController ()
/** lastOffsetX */
@property(nonatomic,assign) CGFloat lastOffsetX;
/** guide */
@property(nonatomic,weak) UIImageView *guideView;
/** guideLargeText */
@property(nonatomic,weak) UIImageView *guideLargeView;
/** guideSmallText */
@property(nonatomic,weak) UIImageView *guideSmallView;
@end // CollectionViewController的结构层次:首先是控制器的View 上面是collectionView
// self.view != self.collectionView // 1.初始化的时候必须设置布局参数,通常使用系统提供的流水布局UICollectionViewFlowLayout
// 2.cell必须通过注册
// 3.自定义cell @implementation ChaosNewFeatureViewController static NSString * const ID = @"Cell"; - (instancetype)init
{
// 设置流水布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; // 设置布局方向
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 设置布局中每个cell的尺寸
layout.itemSize = ChaosScreenBounds.size;
// 设置每个cell水平之间的间距
// layout.minimumInteritemSpacing = 0;
// 设置每行之间的间距
layout.minimumLineSpacing = ;
// 设置周边的间距
// layout.sectionInset = UIEdgeInsetsMake(20, 20, 0, 20); return [super initWithCollectionViewLayout:layout];
} - (void)viewDidLoad {
[super viewDidLoad];
// 这个颜色我们看不见,因为上面还有一个self.collectionView
self.view.backgroundColor = [UIColor redColor];
// 看到的颜色是这个
self.collectionView.backgroundColor = [UIColor greenColor];
// 水平滚动条是否显示
self.collectionView.showsHorizontalScrollIndicator = NO;
// 是否启用分页
self.collectionView.pagingEnabled = YES;
// 是否启用弹簧效果
self.collectionView.bounces = NO; // 注册cell
[self.collectionView registerClass:[ChaosNewFeatureCell class] forCellWithReuseIdentifier:ID]; // 添加所有ImageView
[self setUpAllChildrenView];
} - (void)setUpAllChildrenView
{
// 添加guide
UIImageView *guide = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide1"]]; guide.centerX = self.collectionView.centerX;
guide.centerY = self.collectionView.centerY; _guideView = guide;
[self.collectionView addSubview:guide]; // 添加guideLine
UIImageView *guideLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLine"]]; guideLine.x -= ; [self.collectionView addSubview:guideLine]; // 添加guideLargeText
UIImageView *guideLargeText = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLargeText1"]]; guideLargeText.centerX = self.collectionView.centerX;
guideLargeText.centerY = self.collectionView.height * 0.7; _guideLargeView = guideLargeText;
[self.collectionView addSubview:guideLargeText]; // 添加guideSmallText
UIImageView *guideSmallText = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideSmallText1"]]; guideSmallText.centerX = self.collectionView.centerX;
guideSmallText.centerY = self.collectionView.height * 0.8; _guideSmallView = guideSmallText;
[self.collectionView addSubview:guideSmallText];
} // 滚动减速结束的时候调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat curOffsetX = scrollView.contentOffset.x; // 每次移动的距离就是现在的offsetX 与 上次offsetX 的差值,这样的话左右都可以
CGFloat delta = curOffsetX - _lastOffsetX; // 先让图片移动到两倍的间距,然后以动画形式移动过来
_guideView.x += * delta;
_guideLargeView.x += * delta;
_guideSmallView.x += * delta; // 计算当前页数
NSInteger pageNO = curOffsetX / self.view.width + ;
// 拼接图片名称
NSString *guideImage = [NSString stringWithFormat:@"guide%ld",pageNO];
NSString *largeTextImage = [NSString stringWithFormat:@"guideLargeText%ld",pageNO];
NSString *smallTextImage = [NSString stringWithFormat:@"guideSmallText%ld",pageNO];
// 修改图片
_guideView.image = [UIImage imageNamed:guideImage];
_guideLargeView.image = [UIImage imageNamed:largeTextImage];
_guideSmallView.image = [UIImage imageNamed:smallTextImage]; // 记录上次的offsetX
_lastOffsetX = curOffsetX; // 动画
[UIView animateWithDuration:0.25 animations:^{
_guideView.x -= delta;
_guideLargeView.x -= delta;
_guideSmallView.x -= delta;
}];
} // 告诉collectionView有几个部分
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return ;
}
// 告诉collectionView 的第 Section 部分有几个cell 也就是item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return ;
}
// 告诉collectionView 中每个cell长什么样
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{ ChaosNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
NSString *imageName = [NSString stringWithFormat:@"guide%ldBackground",indexPath.item + ];
cell.image = [UIImage imageNamed:imageName]; return cell;
}
@end
iOS彩票项目--第四天,新特性界面搭建,UICollectionViewController的初次使用的更多相关文章
- [iOS微博项目 - 1.7] - 版本新特性
A.版本新特性 1.需求 第一次使用新版本的时候,不直接进入app,而是展示新特性界面 github: https://github.com/hellovoidworld/HVWWeibo ...
- iOS彩票项目--第五天,新特性引导页的封装、返回按钮的自定义、导航控制器的滑动返回以及自定义滑动返回功能
一.上次实现了在AppDelegate中通过判断app版本决定是否进入新特性页面,今天将AppDelegate中的一坨进行了封装.将self.window的根控制器到底应该为新特性界面,还是主页面,封 ...
- ES6系列之项目中常用的新特性
ES6系列之项目中常用的新特性 ES6常用特性 平时项目开发中灵活运用ES6+语法可以让开发者减少很多开发时间,提高工作效率.ES6版本提供了很多新的特性,接下来我列举项目中常用的ES6+的特性: l ...
- iOS 之新特性界面
1.什么事新特性界面? 新特性界面就是第一次下载程序出现的界面,他的用途是帮助用户快速了解这款APP,所有说还是很有必要学一下的. 2.如何实现新特性界面? 实现思路:从本质上看,新特性界面就是一个全 ...
- 【转载】Android N 完全不同以往的四个新特性
Google最近发布了Android的下一个版本,Android N的开发者预览版.此次预览版,可以让我们开发者在正式发布之前就测试代码,包括一些新的API,甚至于也可以提前反馈那些对于我们来说有些困 ...
- IOS第三天-新浪微博 - 版本新特性,OAuth授权认证
*********版本新特性 #import "HWNewfeatureViewController.h" #import "HWTabBarViewController ...
- iOS 10的23个隐藏新特性-b
上周iOS 10正式版推送后,24小时的更新率已经超过15%,实在惊人.虽然有着初期变砖.5S6卡顿.移动VoLTE无法使用.美版无信号等BUG,但不可忽视的是,iOS 10还是带来了很多从前没有的功 ...
- iOS彩票项目--第三天,搭建竞技场和发现,搭建幸运选号和我的彩票界面
一.竞技场搭建--UISegmentedControl的使用 // 重写 自定义控制器的view - (void)loadView { UIImageView *imgView = [[UIImage ...
- [iOS微博项目 - 2.1] - 获得新浪授权接口
A.如何获得新浪的授权接口 登陆新浪的开放平台 注册新浪账号 创建应用 获得应用id和请求地址 查阅相关API 关联需要进行测试的账号 1.登陆开放平台 http://open.weibo.com ...
随机推荐
- Android HTTP通讯
这里有一个非常棒的http通讯的总结,我看了以后茅塞顿开. 先贴代码: 01 public class Activity1 extends Activity { 02 03 private ...
- memset(&a, 0, sizeof(struct customer))函数
memset(&a, 0, sizeof(struct customer))函数定义在memory.h中,用于给指定的内存区域赋值,在该语句中,&a指定待赋值的内存首地址,0是要赋的值 ...
- URL 长度有限制吗?
众所周知,传递小量参数(在没有其他原因,例如隐藏参数值的情况下)推荐使用GET方法,传递大量参数推荐使用POST方法.原因是什么呢? 原因是传说GET方法是通过URL来传递,而URL的长度是受限的,而 ...
- 修改 Input placeholder 的样式
::-webkit-input-placeholder { /* WebKit browsers */ color: #ccc; } :-moz-placeholder { /* Mozilla Fi ...
- Latex学习——长竖线及长括号
Latex学习——长竖线及长括号 文章修改中要求把花括号和竖线变长,查了下发现下面的几种方法: 1.花括号“{ }”变长: \$ \left\{... content...... ...
- ThreadLocal 类说明
1 ThreadLocal 不是一个线程,而是保存线程本地化对象的容器.当运行于多线程环境的某个对象使用 ThreadLocal 维护变量时,ThreadLocal 为每一个使用该变量的线程分配一个独 ...
- 【转】用SQL实现树的查询
树形结构是一类重要的非线性结构,在关系型数据库中如何对具有树形结构的表进行查询,从而得到所需的数据是一个常见的问题.本文笔者以 SQL Server 2000 为例,就一些常用的查询给出了相应的算法与 ...
- guava -- 新集合类型
Guava引入了很多JDK没有的.但有用的新集合类型.这些新类型是为了和JDK集合框架共存,而没有往JDK集合抽象中硬塞其他概念. 作为一般规则,Guava集合非常精准地遵循了JDK接口契约. 1. ...
- C++11 容器Array
array是一个固定大小的顺序容器,不能动态改变大小,array内的元素在内存中以严格的线性顺序存储与普通数组声明存储空间大小[]的方式是一样有效的,只是加入了一些成员函数和全局函数[get (arr ...
- iOS登录单例
iOS登录单例 一,工程图. 二,代码. UserInfo.h #import <Foundation/Foundation.h> @interface UserInfo : NSObje ...