Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView setLayout:已设置动画:不保留zIndex_Ios_Uicollectionview_Z Index - Fatal编程技术网

Ios UICollectionView setLayout:已设置动画:不保留zIndex

Ios UICollectionView setLayout:已设置动画:不保留zIndex,ios,uicollectionview,z-index,Ios,Uicollectionview,Z Index,我注意到,在UICollectionView中调用setLayout:animated在两个布局之间切换时,当前可见的单元格不符合zIndex它的布局属性已在layouttributesforitematindexpath:中设置 例如,如果要使用UICollectionViewFlowLayout创建一个UICollectionView,请将其minimumLineSpacing设置为负数,使单元格重叠,然后在每个单元格上设置一个高于上一个单元格的zIndex,这样看起来就像是从下往上堆叠单元

我注意到,在
UICollectionView
中调用
setLayout:animated
在两个布局之间切换时,当前可见的单元格不符合
zIndex
它的布局属性已在
layouttributesforitematindexpath:
中设置

例如,如果要使用
UICollectionViewFlowLayout
创建一个
UICollectionView
,请将其
minimumLineSpacing
设置为负数,使单元格重叠,然后在每个单元格上设置一个高于上一个单元格的
zIndex
,这样看起来就像是从下往上堆叠单元格一样


但是,如果我将布局设置为另一个布局,然后返回到该原始布局,则会中断。这就好像当前可见的单元不监听zIndex,而是放在其他单元的顶部。如果我将单元格滚动到屏幕外,则返回到正确的位置。

尝试在以下位置设置z索引:

- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath;
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath;

我也有同样的问题。切换布局将忽略单元的zIndex

我通过在z轴上应用如下平移,使其“看起来正确”:

attributes.transform3D = CATransform3DMakeTranslation(0, 0, indexPath.row);

但这只是一个视觉修复,如果你试图点击该项目,你会意识到zIndex仍然是错误的,直到它被滚动到屏幕外循环使用。

我已经通过grimfrog和Charlie Elliott的组合反应获得了我想要的行为

Charlie Elliott的解决方案为集合视图中的项目获得了正确的最终结果,但在动画期间zIndex上仍然存在捕捉效果

grimfrog的解决方案提供了正确的外观,但在布局更改后,尽管zIndex看起来正确,但仍然存在不正确的问题

两者的结合虽然不是一个很好的解决方案,但确实有效,并且使用了UICollectionViewLayoutAttribute支持的transform和zIndex属性

在我的布局中,我有

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
  NSArray *attributes = [super layoutAttributesForElementsInRect:rect];

  [attributes enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes *attributes, NSUInteger idx, BOOL *stop) {
    attributes.zIndex = attributes.indexPath.item + 1;
  }];

  return attributes;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
  UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForItemAtIndexPath:indexPath];

  attributes.transform3D = CATransform3DMakeTranslation(0, 0, attributes.indexPath.item);
  return attributes;
}

我现在还不想把这个作为正确的答案,因为我确信一定有其他的方法来解决这个问题,但我很想看看这是否也能解决其他人的问题。

这也让我有点头疼。经过几次测试后,我意识到无论z-指数如何,
UICollectionView
都会强制选定的单元格位于顶部。

使用@sampage和@grimprog answers作为起点,我能够得到类似的情况

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path
{
    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:path];
    attributes.zIndex = path.item;
    attributes.transform3D = CATransform3DMakeTranslation(0, 0, path.item);
    // other attribute settings
    return attributes;
}
我的
layouttributesforelementsinrect:
在生成属性数组时调用
layouttributesforitematIndexPath:
,因此我只需要在其中包含zIndex和transform3D。

尝试:

// In UICollectionViewCell subclass
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    // Setting zPosition instead of relaying on
    // UICollectionView zIndex management 'fixes' the issue
    self.layer.zPosition = layoutAttributes.zIndex;
}

我有另一个解决办法。由于所有单元格都属于同一个superview,因此在单元格显示正常时调用
会将SubViewToFront:
。具体地说,通过查看调试视图层次结构,尽管UICollectionViewLayout没有根据zIndex渲染单元格,但它仍然按照每个子视图添加到其超级视图的相反顺序显示单元格。

感谢您的回复,尽管我对您的解决方案没有任何了解。我认为问题在于UICollectionView对所选单元格的zIndex进行了处理。我可能错了,但是如果单元格已经可见并且在整个布局更改过程中保持可见,则更改布局可能不会调用AppearinGiteMatIndex的
InitialLayoutAttributes。我刚刚制作了一个快速示例,并能够用Z索引重现问题。据我所知,在失效(动画边界更改、批更新块)后,屏幕上的每个项目都会调用
InitialLayoutAttribute for AppearinGiteMatIndex
,但在重新读取文档后,这些项目需要设置动画。不过,令人高兴的是,通过在
layouttributesforementsinrect:
中设置z-index,而不是在
layouttributesforementindex:
@sampage中设置z-index,我在切换布局时能够获得一致的结果。您能告诉我更多关于所选单元格的z-index吗?我找不到任何地方证明你说的话,但我确实注意到我的动画中有一些不一致的地方。。。谢谢如果你的答案不是一个实际的解决方案,我建议你把它作为一个评论来发布。很抱歉,我不熟悉堆栈溢出的礼节。我不知道如何发表评论,但我能找到的只是“添加另一个答案”。你完全正确:需要50分的声誉。我不是故意推你的。欢迎合作。我的评论是邀请大家以这种方式合作。:)如果您将它与zindex-ie.
attributes.transform3D=catTransformorm3dmaketransation(0,0,indexPath.row)并排使用,它就是一个解决方案;attributes.zIndex=indexPath.row-至少使用这两种方法对我有效。这对我有效,但我不确定为什么必须在layoutAttributesForItemAtIndexPath中设置transform3D。如果仅在LayoutAttributesForElementsRect中设置,则它不起作用。