ios – 在UICollectionView布局转换中完善子视图大小调整

前端之家收集整理的这篇文章主要介绍了ios – 在UICollectionView布局转换中完善子视图大小调整前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个具有UICollectionView的应用程序 – 集合视图的工作是显示来自Web服务的数据.

我试图实现的应用程序的一个功能是使用户能够将此UICollectionView的布局从网格视图更改为表视图.

我花了很多时间尝试完善这个工作,让它工作,我设法让它工作.
但是有一些问题.两个布局之间的转换看起来不太好,有时在切换视图之间会中断,而我的应用程序仍然处于意外状态.只有当用户非常快速地切换网格和表格视图(按changeLayoutButton)时才会发生.

所以,显然有一些问题,我觉得代码有点..脆弱.我也需要解决上述问题.

我会开始,我实现了这个观点.

履行

由于我需要两个不同的单元格(grideCell和tableViewCell)来显示不同的东西 – 我决定更好地子类化UICollectionViewFlowLayout,因为它执行了所有我需要的 – 我需要做的只是改变单元格的大小.

考虑到这一点,我创建了两个类,它们分类为UICollectionViewFlowLayout

这就是两个班的外观:

BBTradeFeedTableViewLayout.m

  1. #import "BBTradeFeedTableViewLayout.h"
  2.  
  3. @implementation BBTradeFeedTableViewLayout
  4.  
  5. -(id)init
  6. {
  7. self = [super init];
  8.  
  9. if (self){
  10.  
  11. self.itemSize = CGSizeMake(320,80);
  12. self.minimumLineSpacing = 0.1f;
  13. }
  14.  
  15. return self;
  16. }
  17.  
  18. @end

BBTradeFeedGridViewLayout.m

  1. #import "BBTradeFeedGridViewLayout.h"
  2.  
  3. @implementation BBTradeFeedGridViewLayout
  4.  
  5. -(id)init
  6. {
  7. self = [super init];
  8.  
  9. if (self){
  10.  
  11. self.itemSize = CGSizeMake(159,200);
  12. self.minimumInteritemSpacing = 2;
  13. self.minimumLineSpacing = 3;
  14. }
  15. return self;
  16. }
  17.  
  18. @end

很简单,你可以看到 – 只是改变单元格大小.

然后在我的viewControllerA类中,我实现了这样的UICollectionView:
创建属性

  1. @property (strong,nonatomic) BBTradeFeedTableViewLayout *tableViewLayout;
  2. @property (strong,nonatomic) BBTradeFeedGridViewLayout *grideLayout;

在viewDidLoad

  1. /* Register the cells that need to be loaded for the layouts used */
  2. [self.tradeFeedCollectionView registerNib:[UINib nibWithNibName:@"BBItemTableViewCell" bundle:nil] forCellWithReuseIdentifier:@"TableItemCell"];
  3. [self.tradeFeedCollectionView registerNib:[UINib nibWithNibName:@"BBItemGridViewCell" bundle:nil] forCellWithReuseIdentifier:@"GridItemCell"];

用户点击按钮在布局之间切换:

  1. -(void)changeViewLayoutButtonPressed

我使用BOOL来确定哪个布局当前处于活动状态,并且基于我使用此代码进行切换:

  1. [self.collectionView performBatchUpdates:^{
  2.  
  3. [self.collectionView.collectionViewLayout invalidateLayout];
  4. [self.collectionView setCollectionViewLayout:self.grideLayout animated:YES];
  5.  
  6. } completion:^(BOOL finished) {
  7. }];

在cellForItemAtIndexPath中

我确定我应该使用哪个单元格(grid或tableView)和加载数据 – 代码如下所示:

  1. if (self.gridLayoutActive == NO){
  2.  
  3. self.switchToTableLayout = NO;
  4. BBItemTableViewCell *tableItemCell = [collectionView dequeueReusableCellWithReuseIdentifier:tableCellIdentifier forIndexPath:indexPath];
  5.  
  6. if ([self.searchArray count] > 0){
  7.  
  8. self.switchToTableLayout = NO;
  9.  
  10. tableItemCell.gridView = NO;
  11. tableItemCell.backgroundColor = [UIColor whiteColor];
  12. tableItemCell.item = self.searchArray[indexPath.row];
  13.  
  14. }
  15. return tableItemCell;
  16. }else
  17.  
  18. {
  19.  
  20. BBItemTableViewCell *gridItemCell= [collectionView dequeueReusableCellWithReuseIdentifier:gridCellIdentifier forIndexPath:indexPath];
  21.  
  22. if ([self.searchArray count] > 0){
  23.  
  24. self.switchToTableLayout = YES;
  25.  
  26. gridItemCell.gridView = YES;
  27. gridItemCell.backgroundColor = [UIColor whiteColor];
  28. gridItemCell.item = self.searchArray[indexPath.row];
  29.  
  30. }
  31.  
  32. return gridItemCell;
  33. }

最后在两个单元格类 – 我只是使用数据来设置我需要的图像/文本.

在网格单元格中,图像越大,我删除不想要的文本,这是使用两个单元格的主要原因.

我真的很感激一些帮助,如何解决我目前的问题,并改善这种观点,看起来更流畅,更少..在UI中的bug.我要去的样子就像eBay iOS应用程序 – 它们在三种不同的视图之间切换.我只需要在两个不同的视图之间切换.

解决方法

网格/表格转换不会像您所相信的简单的演示一样容易.当您在单元格的中间有一个单一的标签和坚实的背景时,它们可以正常工作,但一旦你有任何真实的内容,它就会崩溃.这就是为什么:

>你无法控制动画的时间和性质.
>虽然布局中的单元格的框架从一个值到另一个值是动画的,但单元本身(特别是如果您使用两个单独的单元格)似乎不对动画的每个步骤执行内部布局,因此似乎从每个单元格中的一个布局“下滑”到下一个布局 – 您的网格单元在表格大小上看起来错误,反之亦然.

有很多不同的解决方案.很难推荐任何具体的内容,但没有看到您的单元格的内容,但我已经取得了以下成功:

>使用如here所示的技术控制动画.您还可以查看Facebook Pop来更好地控制转换,但我没有仔细研究.
>对于这两个布局使用相同的单元格.在layoutSubviews中,计算从一个布局到另一个布局的过渡距离,并将其用于淡出或未使用的元素,并为其他元素计算好的过渡帧.这可以防止从一个单元格类别到另一个单元格类型的抖动开关.

这是我使用here方法效果相当不错.

依靠调整面具或Autolayout的大小,这是更难的工作,但这是额外的工作,使事情看起来不错.

对于用户可以在布局之间切换太快的问题,只需在转换开始时禁用该按钮,并在完成后重新启用它.

作为一个更实际的例子,这里是从上面链接的应用程序的布局变化(一些省略)的示例.注意,在转换发生时,交互被禁用,我正在使用从上面链接的项目中的转换布局,并且有一个完成处理程序:

  1. -(void)toggleLayout:(UIButton*)sender
  2. {
  3. [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
  4.  
  5. HMNNewsLayout newLayoutType = self.layoutType == HMNNewsLayoutTable ? HMNNewsLayoutGrid : HMNNewsLayoutTable;
  6.  
  7. UICollectionViewLayout *newLayout = [HMNNewsCollectionViewController collectionViewLayoutForType:newLayoutType];
  8.  
  9. HMNTransitionLayout *transitionLayout = (HMNTransitionLayout *)[self.collectionView transitionToCollectionViewLayout:newLayout duration:0.5 easing:QuarticEaseInOut completion:^(BOOL completed,BOOL finish)
  10. {
  11. [[NSUserDefaults standardUserDefaults] setInteger:newLayoutType forKey:HMNNewsLayoutTypeKey];
  12. self.layoutType = newLayoutType;
  13. sender.selected = !sender.selected;
  14. for (HMNNewsCell *cell in self.collectionView.visibleCells)
  15. {
  16. cell.layoutType = newLayoutType;
  17. }
  18. [[UIApplication sharedApplication] endIgnoringInteractionEvents];
  19. }];
  20.  
  21. [transitionLayout setUpdateLayoutAttributes:^UICollectionViewLayoutAttributes *(UICollectionViewLayoutAttributes *layoutAttributes,UICollectionViewLayoutAttributes *fromAttributes,UICollectionViewLayoutAttributes *toAttributes,CGFloat progress)
  22. {
  23. HMNTransitionLayoutAttributes *attributes = (HMNTransitionLayoutAttributes *)layoutAttributes;
  24. attributes.progress = progress;
  25. attributes.destinationLayoutType = newLayoutType;
  26. return attributes;
  27. }];
  28. }

在单元格中,这是任一布局的相同单元格,我有一个图像视图和一个标签容器.标签容器保留所有标签,并使用自动布局将其放在内部.每个布局中的图像视图和标签容器都有不变的框架变量.

过渡布局中的布局属性是一个自定义子类,其中包含在上述更新布局属性块中设置的转换进度属性.这使用applyLayoutAttributes方法(其他一些代码省略)传递到单元格中:

  1. -(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
  2. {
  3. self.transitionProgress = 0;
  4. if ([layoutAttributes isKindOfClass:[HMNTransitionLayoutAttributes class]])
  5. {
  6. HMNTransitionLayoutAttributes *attributes = (HMNTransitionLayoutAttributes *)layoutAttributes;
  7. self.transitionProgress = attributes.progress;
  8. }
  9. [super applyLayoutAttributes:layoutAttributes];
  10. }

layout在细胞子类中的浏览在图像和标签的两个帧之间进行内插的艰巨工作,如果正在进行转换:

  1. -(void)layoutSubviews
  2. {
  3. [super layoutSubviews];
  4.  
  5. if (!self.transitionProgress)
  6. {
  7. switch (self.layoutType)
  8. {
  9. case HMNNewsLayoutTable:
  10. self.imageView.frame = imageViewTableFrame;
  11. self.labelContainer.frame = labelContainerTableFrame;
  12. break;
  13. case HMNNewsLayoutGrid:
  14. self.imageView.frame = imageViewGridFrame;
  15. self.labelContainer.frame = self.originalGridLabelFrame;
  16. break;
  17. }
  18. }
  19. else
  20. {
  21. CGRect fromImageFrame,toImageFrame,fromLabelFrame,toLabelFrame;
  22. if (self.layoutType == HMNNewsLayoutTable)
  23. {
  24. fromImageFrame = imageViewTableFrame;
  25. toImageFrame = imageViewGridFrame;
  26. fromLabelFrame = labelContainerTableFrame;
  27. toLabelFrame = self.originalGridLabelFrame;
  28. }
  29. else
  30. {
  31. fromImageFrame = imageViewGridFrame;
  32. toImageFrame = imageViewTableFrame;
  33. fromLabelFrame = self.originalGridLabelFrame;
  34. toLabelFrame = labelContainerTableFrame;
  35. }
  36.  
  37. CGFloat from = 1.0 - self.transitionProgress;
  38. CGFloat to = self.transitionProgress;
  39.  
  40. self.imageView.frame = (CGRect)
  41. {
  42. .origin.x = from * fromImageFrame.origin.x + to * toImageFrame.origin.x,.origin.y = from * fromImageFrame.origin.y + to * toImageFrame.origin.y,.size.width = from * fromImageFrame.size.width + to * toImageFrame.size.width,.size.height = from * fromImageFrame.size.height + to * toImageFrame.size.height
  43. };
  44.  
  45. self.labelContainer.frame = (CGRect)
  46. {
  47. .origin.x = from * fromLabelFrame.origin.x + to * toLabelFrame.origin.x,.origin.y = from * fromLabelFrame.origin.y + to * toLabelFrame.origin.y,.size.width = from * fromLabelFrame.size.width + to * toLabelFrame.size.width,.size.height = from * fromLabelFrame.size.height + to * toLabelFrame.size.height
  48. };
  49. }
  50. self.headlineLabel.preferredMaxLayoutWidth = self.labelContainer.frame.size.width;
  51. }

就是这样.基本上,您需要一种告诉单元格的过程有多远,您需要布局转换库(或者像我所说的Facebook pop可能会这样做),然后您需要确保您获得不错的值用于在两者之间转换时进行布局.

猜你在找的iOS相关文章