gpt4 book ai didi

ios - UITableViewCell 阴影在滚动后消失

转载 作者:技术小花猫 更新时间:2023-10-29 11:00:08 54 4
gpt4 key购买 nike

我将 UITableViewCell 类子类化以在我的单元格下方添加阴影。当 TableView 出现在屏幕上时,阴影已正确添加。但是,当我向下滚动 tableview,并且带有阴影的单元格隐藏在屏幕上方时,阴影消失了。

- (void)layoutSubviews {
[super layoutSubviews];
if (self.shouldAddShadow) {
self.layer.shadowOpacity = 0.5;
self.layer.shadowRadius = 1.5;
self.layer.shadowOffset = CGSizeMake(0, 3);
self.layer.shadowColor = [[[UIColor appDarkDividerColor] colorWithAlphaComponent:0.9] CGColor];
[self setClipsToBounds:NO];
[self.layer setMasksToBounds:NO];
CGRect shadowFrame = self.layer.bounds;
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath;
self.layer.shadowPath = shadowPath;
}
}

忘了说,我有带静态单元格的表格 View ;所以 prepareForReuse 不会被调用。我的单元格有 socket ,所以我也尝试在 scrollViewDidScroll: 方法中为我的单元格设置阴影。即使这样也帮不了我

最佳答案

我刚遇到这个问题。最后我找到了让它工作的方法。

它并没有消失(移除),它只是被隐藏了。

我们使用单元格层的属性 zPosition

From Apple docs:

The default value of this property is 0. Changing the value of this property changes the the front-to-back ordering of layers onscreen. This can affect the visibility of layers whose frame rectangles overlap.

The value of this property is measured in points.

默认值为0。这导致顶部单元格隐藏底部单元格(比如阴影)。这意味着如果您为 View 设置阴影以使其显示在两侧并且两个单元格之前的边距为零,则只会显示顶部单元格的底部阴影,底部阴影的顶部阴影将被隐藏。

当单元格离开屏幕然后返回时,虽然每个单元格的 zPosition 仍然是 0,但对于这些单元格,底部单元格现在隐藏了顶部单元格。隐藏方向与滚动方向相反。这正是你遇到的情况。

所以,

cell.layer.zPosition = <#value you want#>

比如我想显示 sibling 的影子,我可以将这个单元格图层的zPosition设置为-1,这样就会出现两边的影子。

层的

zPosition 决定哪个单元格可以显示在前面,哪个单元格可以显示在后面。类似于 CSS 中的 z-index

因此解决方案是更改 zPosition 属性,使其按预期工作。


此外,您不应将单元格的clipsToBounds 设置为YES。 (默认值为否)

关于ios - UITableViewCell 阴影在滚动后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31048337/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com