概述

UICollectionView真的好强大,今天我们来研究一下这种很常见的卡片动画效果是如何实现了。本篇不能太深入地讲解,因为笔者也是刚刚摸索出点眉目,但是并没有深刻地理解。如果在讲解过程中,出现不对的地方,请及时反馈。

效果图

重写API

 
1
2
3
4
5
6
7
8
9
10
11
12
 
// 我们必须重写此方法,指定布局大小
// 每次layout invalidated或者重新query布局信息时,会调用
- (void)prepareLayout;
 
// 用于决定布局信息
// 我们必须重写它来实现布局信息
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect;
 
// 重写它来布局信息
- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
 

还有一个非常关键的API,必须重写:

 
1
2
3
4
5
 
// return YES to cause the collection view to requery the layout for geometry information
// 当重新查询布局信息时,就会调用此API。要设置为YES,才能实现自定义布局。
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds;
 

自定义布局

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
 
//
//  HYBCardFlowLayout.m
//  CollectionViewDemos
//
//  Created by huangyibiao on 16/3/26.
//  Copyright © 2016年 huangyibiao. All rights reserved.
//
 
#import "HYBCardFlowLayout.h"
 
@interface HYBCardFlowLayout ()
 
@property (nonatomic, strong) NSIndexPath *mainIndexPath;
@property (nonatomic, strong) NSIndexPath *willMoveToMainIndexPath;
 
@end
 
@implementation HYBCardFlowLayout
 
- (void)prepareLayout {
  CGFloat inset = 32;
  self.itemSize = CGSizeMake(self.collectionView.frame.size.width - 2 * inset,
                             self.collectionView.frame.size.height * 3 / 4);
  self.sectionInset = UIEdgeInsetsMake(0, inset, 0, inset);
  self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  
  [super prepareLayout];
}
 
#pragma mark - Override
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
  return YES;
}
 
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
  UICollectionViewLayoutAttributes *attribute = [super layoutAttributesForItemAtIndexPath:indexPath];
  
  [self setTransformForLayoutAttributes:attribute];
  
  return attribute;
}
 
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
  NSArray *attributesSuper = [super layoutAttributesForElementsInRect:rect];
  // 一定要深复制一份,不能修改父类的属性,不然会有很多error打印出来
  NSArray *attributes = [[NSArray alloc] initWithArray:attributesSuper copyItems:YES];
  
  NSArray *visibleIndexPaths = [self.collectionView indexPathsForVisibleItems];
  
  if (visibleIndexPaths.count <= 0) {
    return attributes;
  } else if (visibleIndexPaths.count == 1) {
    self.mainIndexPath = [visibleIndexPaths firstObject];
    self.willMoveToMainIndexPath = nil;
  } else if (visibleIndexPaths.count == 2) {
    NSIndexPath *indexPath = [visibleIndexPaths firstObject];
    
    // 说明是往左滚动
    if (indexPath == self.mainIndexPath) {
      // 记录将要移进来的位置
      self.willMoveToMainIndexPath = visibleIndexPaths[1];
    } else {// 往右滚动
      self.willMoveToMainIndexPath = visibleIndexPaths[0];
      
      // 更新下一个成为main
      self.mainIndexPath = visibleIndexPaths[1];
    }
  }
  
  for (UICollectionViewLayoutAttributes *attribute in attributes) {
    [self setTransformForLayoutAttributes:attribute];
  }
  
  return attributes;
}
 
- (void)setTransformForLayoutAttributes:(UICollectionViewLayoutAttributes *)attribute {
  UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:attribute.indexPath];
  
  if (self.mainIndexPath && attribute.indexPath.section == self.mainIndexPath.section) {
    attribute.transform3D = [self tranformForView:cell];
  } else if (self.willMoveToMainIndexPath && attribute.indexPath.section == self.willMoveToMainIndexPath.section) {
    attribute.transform3D = [self tranformForView:cell];
  }
}
 
- (CATransform3D)tranformForView:(UICollectionViewCell *)view {
  // cell的起始偏移
  CGFloat w = self.collectionView.frame.size.width;
  CGFloat offset = [self.collectionView indexPathForCell:view].section * w;
  
  // 当前偏移
  CGFloat currentOffset = self.collectionView.contentOffset.x;
  
  // 计算偏移angle
  CGFloat angle = (currentOffset - offset) / w;
  
  CATransform3D t = CATransform3DIdentity;
  t.m34 = 1.0 / -500;
  
  if (currentOffset - offset >= 0) {
    t = CATransform3DRotate(t, angle, 1, 1, 0);
  } else {
    t = CATransform3DRotate(t, angle, -1, 1, 0);
  }
  
  return t;
}
 
@end
 

这里主要是要处理旋转。然后要处理切换cell的attribute设置。mainIndexPath属性用于记录当前显示的cell的位置。willMoveToMainIndexPath记录将要出现的cell的位置。

结尾

这里在慢慢切换时,效果是挺好的,但是如果快速切换卡片,你会发现会有一点点不好之处,就是下一个cell突然出现的。

CollectionView旋转水平卡片布局的更多相关文章

  1. CollectionView缩放水平卡片布局

    实现效果 实现思路 从Demo效果图中,可以看出来,主要是缩放系数的计算.对于不同距离的cell,其缩放系数要变化,以便整体协调显示. 所以,我们必须重写-layoutAttributesForEle ...

  2. CollectionView垂直缩放卡片布局

    实现效果 实现思路 从效果图可以看到变化是,越是往中间滚动的item显示最大,越显眼.而越是往前面,或者越是后面的,反而显示越小,这样就形成了视觉差. 实现的思路就是通过重写在可见范围内的所有item ...

  3. Java基础之创建窗口——使用卡片布局管理器(TryCardLayout)

    控制台程序. 卡片布局管理器会生成一叠组件——一个组件放在另一个组件的上面.添加到容器中的第一个组件在堆栈的顶部,因此是可见的,添加的最后一个组件在堆栈的底部.使用默认的构造函数CardLayout( ...

  4. 转:三十二、Java图形化界面设计——布局管理器之CardLayout(卡片布局)

    转:http://blog.csdn.net/liujun13579/article/details/7773945 卡片布局能够让多个组件共享同一个显示空间,共享空间的组件之间的关系就像一叠牌,组件 ...

  5. 三十二、Java图形化界面设计——布局管理器之CardLayout(卡片布局)

    摘自 http://blog.csdn.net/liujun13579/article/details/7773945 三十二.Java图形化界面设计--布局管理器之CardLayout(卡片布局) ...

  6. 布局管理器之CardLayout(卡片布局管理器)

    对于选项卡这个概念大家可能不会陌生,就是在一个窗口中可以切换显示多页不同的内容,但同一时间只能是其中的某一页可见的,这样的一个个的页面就是选项卡. CardLayout就是类似的这样一个布局管理器,它 ...

  7. 技术胖Flutter第三季-18布局CardWidget 卡片布局组件

    技术胖Flutter第三季-18布局CardWidget 卡片布局组件 博客地址: https://jspang.com/post/flutter3.html#toc-420 最外面是Card布局,里 ...

  8. 慕课网5-2编程练习:flex布局制作卡片布局案例

    慕课网5-2编程练习:flex布局制作卡片布局案例 小伙伴们,学习了卡片布局,接下来我们根据效果图,也写出一个卡片布局的页面吧! 效果图如下: 任务 1.主体内容的卡片一行只能显示两个. 2.卡片与卡 ...

  9. 设备旋转,创建水平模式布局--Android studio

    1.在项目工具窗口中,右键单击res目录后选择new--Android resource directory菜单项. 2.从资源类型Resource type列表中选择layout,保持Source ...

随机推荐

  1. 五、PL/SQL循环、游标、函数和过程

    --PL/SQL基础知识学习 --一.PL/SQL语句块,基础语法格式 DECLARE --变量声明列表 info varchar(25); --变量声明 stu_unm integer := 15; ...

  2. Linux vi替换字符串

    1. 基本的替换 :s/vivian/sky/ 替换当前行第一个 vivian 为 sky :s/vivian/sky/g 替换当前行所有 vivian 为 sky :n,$s/vivian/sky/ ...

  3. 【转】关于大型网站技术演进的思考(十九)--网站静态化处理—web前端优化—上(11)

    网站静态化处理这个系列马上就要结束了,今天我要讲讲本系列最后一个重要的主题web前端优化.在开始谈论本主题之前,我想问大家一个问题,网站静态化处理技术到底是应该归属于web服务端的技术范畴还是应该归属 ...

  4. Spring Boot配置方式

    Spring提供了xml.注解.Java配置.groovy配置实现Bean的创建和注入. 配置元数据 无论xml配置.注解配置还是Java配置,都被称为配置元数据,所谓元数据即描述数据的数据.元数据本 ...

  5. 只有代码不会撒谎,如何通过Spring boot源码查看其对于各个框架的默认配置

    我发现很多开发对于看源码都有种恐惧心理,其实不必这样,大部分优秀的源码写的都挺直观的,很多时候,你在搜索引擎上搜到的一些东西并不一定是对的,但源码肯定造不了假,毕竟不管你怎么想,它就在那里,该是什么意 ...

  6. BZOJ2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛

    n<=50000个点的树,求选最多不相邻点的个数. f[i][0]=sigma max(f[j][0],f[j][1]),j为i的儿子 f[i][1]=sigma f[j][0],j同上 死于未 ...

  7. Spring注解 @Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析

    @Repository.@Service.@Controller 这几个是一个类型,其实@Component 跟他们也是一个类型的 Spring 2.5 中除了提供 @Component 注释外,还定 ...

  8. React Native资料汇总

    React Native 官方文档中文版翻译 http://wiki.jikexueyuan.com/project/react-native/homepage.html REACT NATIVE开发 ...

  9. 洛谷——P3353 在你窗外闪耀的星星

    P3353 在你窗外闪耀的星星 题目描述 飞逝的的时光不会模糊我对你的记忆.难以相信从我第一次见到你以来已经过去了3年.我仍然还生动地记得,3年前,在美丽的集美中学,从我看到你微笑着走出教室,你将头向 ...

  10. MongoDB学习day08--Mongoose索引、Mongoose内置方法、扩展Mongoose Model的静态方法和实例方法

    一.Mongoose索引 索引是对数据库表中一列或多列的值进行排序的一种结构, 可以让我们查询数据库变得更快. MongoDB 的索引几乎与传统的关系型数据库一模一样, 这其中也包括一些基本的查询优化 ...