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

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

概述我正在开发一个具有UICollectionView的应用程序 – 集合视图的工作是显示来自Web服务的数据. 我试图实现的应用程序的一个功能是使用户能够将此UICollectionView的布局从网格视图更改为表视图. 我花了很多时间尝试完善这个工作,让它工作,我设法让它工作. 但是有一些问题.两个布局之间的转换看起来不太好,有时在切换视图之间会中断,而我的应用程序仍然处于意外状态.只有当用户非常 我正在开发一个具有UICollectionVIEw的应用程序 – 集合视图的工作是显示来自Web服务的数据.

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

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

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

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

履行

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

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

这就是两个班的外观:

BBTradeFeedtableVIEwLayout.m

#import "BBTradeFeedtableVIEwLayout.h"@implementation BBTradeFeedtableVIEwLayout-(ID)init{    self = [super init];    if (self){        self.itemSize = CGSizeMake(320,80);        self.minimumlinespacing = 0.1f;    }    return self;}@end

BBTradeFeedGrIDVIEwLayout.m

#import "BBTradeFeedGrIDVIEwLayout.h"@implementation BBTradeFeedGrIDVIEwLayout-(ID)init{    self = [super init];    if (self){        self.itemSize = CGSizeMake(159,200);        self.minimumInteritemSpacing = 2;        self.minimumlinespacing = 3;    }    return self; }@end

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

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

@property (strong,nonatomic) BBTradeFeedtableVIEwLayout *tableVIEwLayout;@property (strong,nonatomic) BBTradeFeedGrIDVIEwLayout *grIDeLayout;

在vIEwDIDLoad

/* Register the cells that need to be loaded for the layouts used */    [self.TradeFeedCollectionVIEw registerNib:[UINib nibWithNibname:@"BBItemtableVIEwCell" bundle:nil] forCellWithReuseIDentifIEr:@"tableItemCell"];    [self.TradeFeedCollectionVIEw registerNib:[UINib nibWithNibname:@"BBItemGrIDVIEwCell" bundle:nil] forCellWithReuseIDentifIEr:@"GrIDItemCell"];

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

-(voID)changeVIEwLayoutbuttonpressed

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

[self.collectionVIEw performBatchUpdates:^{        [self.collectionVIEw.collectionVIEwLayout invalIDateLayout];        [self.collectionVIEw setCollectionVIEwLayout:self.grIDeLayout animated:YES];    } completion:^(BOol finished) {    }];

在cellForItemAtIndexPath中

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

if (self.grIDLayoutActive == NO){            self.switchTotableLayout = NO;            BBItemtableVIEwCell *tableItemCell = [collectionVIEw dequeueReusableCellWithReuseIDentifIEr:tableCellIDentifIEr forIndexPath:indexPath];            if ([self.searchArray count] > 0){                self.switchTotableLayout = NO;                tableItemCell.grIDVIEw = NO;                tableItemCell.backgroundcolor = [UIcolor whitecolor];                tableItemCell.item = self.searchArray[indexPath.row];            }            return tableItemCell;        }else        {            BBItemtableVIEwCell *grIDItemCell= [collectionVIEw dequeueReusableCellWithReuseIDentifIEr:grIDCellIDentifIEr forIndexPath:indexPath];            if ([self.searchArray count] > 0){                self.switchTotableLayout = YES;                grIDItemCell.grIDVIEw = YES;                grIDItemCell.backgroundcolor = [UIcolor whitecolor];                grIDItemCell.item = self.searchArray[indexPath.row];            }            return grIDItemCell;        }

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

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

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

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

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

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

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

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

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

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

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

-(voID)toggleLayout:(UIbutton*)sender{    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];    HMNNewsLayout newLayoutType = self.layoutType == HMNNewsLayouttable ? HMNNewsLayoutGrID : HMNNewsLayouttable;    UICollectionVIEwLayout *newLayout = [HMNNewsCollectionVIEwController collectionVIEwLayoutForType:newLayoutType];    HMNTransitionLayout *TransitionLayout = (HMNTransitionLayout *)[self.collectionVIEw TransitionToCollectionVIEwLayout:newLayout duration:0.5 easing:QuarticEaseInOut completion:^(BOol completed,BOol finish)            {                [[NSUserDefaults standardUserDefaults] setInteger:newLayoutType forKey:HMNNewsLayoutTypeKey];                self.layoutType = newLayoutType;                sender.selected = !sender.selected;                for (HMNNewsCell *cell in self.collectionVIEw.visibleCells)                {                    cell.layoutType = newLayoutType;                }                [[UIApplication sharedApplication] endIgnoringInteractionEvents];            }];    [TransitionLayout setUpdateLayoutAttributes:^UICollectionVIEwLayoutAttributes *(UICollectionVIEwLayoutAttributes *layoutAttributes,UICollectionVIEwLayoutAttributes *fromAttributes,UICollectionVIEwLayoutAttributes *toAttributes,CGfloat progress)    {        HMNTransitionLayoutAttributes *attributes = (HMNTransitionLayoutAttributes *)layoutAttributes;        attributes.progress = progress;        attributes.destinationLayoutType = newLayoutType;        return attributes;    }];}

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

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

-(voID)applyLayoutAttributes:(UICollectionVIEwLayoutAttributes *)layoutAttributes{    self.TransitionProgress = 0;    if ([layoutAttributes isKindOfClass:[HMNTransitionLayoutAttributes class]])    {        HMNTransitionLayoutAttributes *attributes = (HMNTransitionLayoutAttributes *)layoutAttributes;        self.TransitionProgress = attributes.progress;    }    [super applyLayoutAttributes:layoutAttributes];}

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

-(voID)layoutSubvIEws{    [super layoutSubvIEws];    if (!self.TransitionProgress)    {        switch (self.layoutType)        {            case HMNNewsLayouttable:                self.imageVIEw.frame = imageVIEwtableFrame;                self.labelContainer.frame = labelContainertableFrame;                break;            case HMNNewsLayoutGrID:                self.imageVIEw.frame = imageVIEwGrIDFrame;                self.labelContainer.frame = self.originalGrIDLabelFrame;                break;        }    }    else    {        CGRect fromImageFrame,toImageFrame,fromLabelFrame,tolabelFrame;        if (self.layoutType == HMNNewsLayouttable)        {            fromImageFrame = imageVIEwtableFrame;            toImageFrame = imageVIEwGrIDFrame;            fromLabelFrame = labelContainertableFrame;            tolabelFrame = self.originalGrIDLabelFrame;        }        else        {            fromImageFrame = imageVIEwGrIDFrame;            toImageFrame = imageVIEwtableFrame;            fromLabelFrame = self.originalGrIDLabelFrame;            tolabelFrame = labelContainertableFrame;        }        CGfloat from = 1.0 - self.TransitionProgress;        CGfloat to = self.TransitionProgress;        self.imageVIEw.frame = (CGRect)        {            .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        };        self.labelContainer.frame = (CGRect)        {            .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        };    }    self.headlineLabel.preferredMaxLayoutWIDth = self.labelContainer.frame.size.wIDth;}

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

总结

以上是内存溢出为你收集整理的ios – 在UICollectionView布局转换中完善子视图大小调整全部内容,希望文章能够帮你解决ios – 在UICollectionView布局转换中完善子视图大小调整所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1097146.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存