iOS*项目--第四天,新特性界面搭建,UICollectionViewController的初次使用

一、新特性界面搭建的思路:

  • 在AppDelegate加载主窗体的时候进行判断程序版本号,直接进入程序或者进入新特性展示界面
  • 取出当前的版本号,与旧的版本号相比较(旧的版本号在进入程序的时候存起来 =》建议偏好设置存储)
  • 版本号不一样,说明当前版本是新版本需要进入新特性介绍,并将版本号存下来
 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 2     
 3     // 初始化主窗体
 4     self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
 5     
 6     UIViewController *rootVC = nil;
 7     
 8     // 判断版本号,决定是否进入新特性展示页面
 9     // 取出当前的版本号,与旧的版本号相比较(旧的版本号在真正进入程序的时候存起来 =》建议偏好设置保存)
10     // 版本号不一样,说明当前的版本是新版本,进入新特性介绍,并保存版本号
11     
12     // 当前版本号,通过info获得
13     NSDictionary *dictInfo = [NSBundle mainBundle].infoDictionary;
14     NSString *curVersion = dictInfo[@"CFBundleShortVersionString"];
15     
16     // 旧版本号,通过读取偏好设置获得
17     NSString *oldVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"ChaosVersionKey"];
18     
19     if ([curVersion isEqualToString:oldVersion]) { // 版本号相等,进入程序
20         rootVC = [[ChaosTabBarController alloc] init];
21         
22         rootVC.view.backgroundColor = [UIColor yellowColor];
23     } else { // 有新版本,进入新特性,保存版本号
24     
25         NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
26         [userDefaults setObject:curVersion forKey:@"ChaosVersionKey"];
27         
28         // 一般使用流式布局--自己用什么布局,最好在内部设置,进行封装
29 //        UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
30     
31         rootVC = [[ChaosNewFeatureViewController alloc] init];
32         
33     }
34     
35     self.window.rootViewController = rootVC;
36     // 显示窗口
37     [self.window makeKeyAndVisible];
38     return YES;
39 }

二、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

  1 #import "ChaosNewFeatureViewController.h"
  2 #import "ChaosNewFeatureCell.h"
  3 
  4 @interface ChaosNewFeatureViewController ()
  5 /** lastOffsetX */
  6 @property(nonatomic,assign) CGFloat lastOffsetX;
  7 /** guide */
  8 @property(nonatomic,weak) UIImageView *guideView;
  9 /** guideLargeText */
 10 @property(nonatomic,weak) UIImageView *guideLargeView;
 11 /** guideSmallText */
 12 @property(nonatomic,weak) UIImageView *guideSmallView;
 13 @end
 14 
 15 // CollectionViewController的结构层次:首先是控制器的View  上面是collectionView
 16 // self.view != self.collectionView
 17 
 18 // 1.初始化的时候必须设置布局参数,通常使用系统提供的流水布局UICollectionViewFlowLayout
 19 // 2.cell必须通过注册
 20 // 3.自定义cell
 21 
 22 @implementation ChaosNewFeatureViewController
 23 
 24 static NSString * const ID = @"Cell";
 25 
 26 - (instancetype)init
 27 {
 28     // 设置流水布局
 29     UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
 30     
 31     // 设置布局方向
 32     layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
 33     
 34     // 设置布局中每个cell的尺寸
 35     layout.itemSize = ChaosScreenBounds.size;
 36     // 设置每个cell水平之间的间距
 37 //    layout.minimumInteritemSpacing = 0;
 38     // 设置每行之间的间距
 39     layout.minimumLineSpacing = 0;
 40     // 设置周边的间距
 41 //    layout.sectionInset = UIEdgeInsetsMake(20, 20, 0, 20);
 42     
 43     return [super initWithCollectionViewLayout:layout];
 44 }
 45 
 46 - (void)viewDidLoad {
 47     [super viewDidLoad];
 48     // 这个颜色我们看不见,因为上面还有一个self.collectionView
 49     self.view.backgroundColor = [UIColor redColor];
 50     // 看到的颜色是这个
 51     self.collectionView.backgroundColor = [UIColor greenColor];
 52     // 水平滚动条是否显示
 53     self.collectionView.showsHorizontalScrollIndicator = NO;
 54     // 是否启用分页
 55     self.collectionView.pagingEnabled = YES;
 56     // 是否启用弹簧效果
 57     self.collectionView.bounces = NO;
 58     
 59     // 注册cell
 60     [self.collectionView registerClass:[ChaosNewFeatureCell class] forCellWithReuseIdentifier:ID];
 61     
 62     // 添加所有ImageView
 63     [self setUpAllChildrenView];
 64 }
 65 
 66 - (void)setUpAllChildrenView
 67 {
 68     // 添加guide
 69     UIImageView *guide = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide1"]];
 70     
 71     guide.centerX = self.collectionView.centerX;
 72     guide.centerY = self.collectionView.centerY;
 73     
 74     _guideView = guide;
 75     [self.collectionView addSubview:guide];
 76     
 77     // 添加guideLine
 78     UIImageView *guideLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLine"]];
 79     
 80     guideLine.x -= 130;
 81     
 82     [self.collectionView addSubview:guideLine];
 83     
 84     // 添加guideLargeText
 85     UIImageView *guideLargeText = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
 86     
 87     guideLargeText.centerX = self.collectionView.centerX;
 88     guideLargeText.centerY = self.collectionView.height * 0.7;
 89     
 90     _guideLargeView = guideLargeText;
 91     [self.collectionView addSubview:guideLargeText];
 92     
 93     // 添加guideSmallText
 94     UIImageView *guideSmallText = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
 95     
 96     guideSmallText.centerX = self.collectionView.centerX;
 97     guideSmallText.centerY = self.collectionView.height * 0.8;
 98     
 99     _guideSmallView = guideSmallText;
100     [self.collectionView addSubview:guideSmallText];
101 }
102 
103 // 滚动减速结束的时候调用
104 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
105 {
106     CGFloat curOffsetX = scrollView.contentOffset.x;
107     
108     // 每次移动的距离就是现在的offsetX 与 上次offsetX 的差值,这样的话左右都可以
109     CGFloat delta = curOffsetX - _lastOffsetX;
110     
111     // 先让图片移动到两倍的间距,然后以动画形式移动过来
112     _guideView.x += 2 * delta;
113     _guideLargeView.x += 2 * delta;
114     _guideSmallView.x += 2 * delta;
115     
116     // 计算当前页数
117     NSInteger pageNO = curOffsetX / self.view.width + 1;
118     // 拼接图片名称
119     NSString *guideImage = [NSString stringWithFormat:@"guide%ld",pageNO];
120     NSString *largeTextImage = [NSString stringWithFormat:@"guideLargeText%ld",pageNO];
121     NSString *smallTextImage = [NSString stringWithFormat:@"guideSmallText%ld",pageNO];
122     // 修改图片
123     _guideView.image = [UIImage imageNamed:guideImage];
124     _guideLargeView.image = [UIImage imageNamed:largeTextImage];
125     _guideSmallView.image = [UIImage imageNamed:smallTextImage];
126     
127     // 记录上次的offsetX
128     _lastOffsetX = curOffsetX;
129     
130     // 动画
131     [UIView animateWithDuration:0.25 animations:^{
132         _guideView.x -= delta;
133         _guideLargeView.x -= delta;
134         _guideSmallView.x -= delta;
135     }];
136 }
137 
138 // 告诉collectionView有几个部分
139 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
140 {
141     return 1;
142 }
143 // 告诉collectionView 的第 Section 部分有几个cell 也就是item
144 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
145 {
146     return 4;
147 }
148 // 告诉collectionView 中每个cell长什么样
149 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
150 {
151     
152     ChaosNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
153     NSString *imageName = [NSString stringWithFormat:@"guide%ldBackground",indexPath.item + 1];
154     cell.image = [UIImage imageNamed:imageName];
155     
156     return cell;
157 }
158 @end
View Code
原文地址:https://www.cnblogs.com/gchlcc/p/5379543.html