Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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 向上滚动时UITableViewCell中的阴影从下至上移动_Ios_Objective C_Uitableview_Scroll_Shadow - Fatal编程技术网

Ios 向上滚动时UITableViewCell中的阴影从下至上移动

Ios 向上滚动时UITableViewCell中的阴影从下至上移动,ios,objective-c,uitableview,scroll,shadow,Ios,Objective C,Uitableview,Scroll,Shadow,我有一个自定义单元格的UITableView,我希望有底部阴影/放置阴影。为此,我使用以下代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueRe

我有一个自定义单元格的UITableView,我希望有底部阴影/放置阴影。为此,我使用以下代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myTableViewCell" forIndexPath:indexPath];

    cell.layer.shouldRasterize = YES;
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
    cell.layer.shadowColor = [[UIColor blackColor] CGColor];
    cell.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
    cell.layer.shadowRadius = 1.0f;
    cell.layer.shadowOpacity = 1.0f;

    CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:cell.bounds].CGPath;
    cell.layer.shadowPath = shadowPath;

    return cell;
}
当tableview向下滚动或根本没有滚动时,阴影看起来与预期的一样:

但是,每当向上滚动时,阴影都会从单元格底部移动到顶部:

我怀疑这与每次滚动tableview时渲染的阴影有关,但我不知道如何解决它

我哪里做错了


事先非常感谢。

您已将阴影设置为围绕整个单元格。根据单元格的绘制顺序,您将看到顶部阴影或底部阴影。设置阴影,使其仅显示在单元格底部,而不是顶部。使用
shadowOffset
向下移动阴影,使其不会出现在单元格顶部。您可能还需要更改阴影半径。

首先,我建议不要在每个单元格中调整单元格层。也就是说,如果有多个屏幕充满了单元格,则会使TableView滚动非常不平滑。 我做了同样的事情,但在一个添加到细胞的ImageView中,这是可怕的。我的解决方案是创建一个包含阴影的UIImage/UIView,并将其添加为子视图,这对阴影不是很好,但一种简单的方法是:

- (UIView*)shadowWithRect:(CGRect)rect {
    UIView* v = [[UIView alloc] initWithFrame:rect];
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = v.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor clearColor] CGColor], nil];
    [v.layer insertSublayer:gradient atIndex:0];
    return v;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"shadowCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell addSubview:[self shadowWithRect:cell.bounds]];
    return cell;
}
也可以将该视图“保存”到属性或其他内容中,以避免每次都重新创建该视图。
另外,不要忘记删除/不添加多个“阴影”,否则它看起来会很奇怪。

谢谢@rmaddy的建议。我将通过调整偏移量和半径来确定是否正确。我将阴影半径设置为3.0f,阴影偏移量设置为CGSizeMake(0.0f,3.0f)。这会导致阴影:A.变厚(如预期的那样)B.向上滚动时消失。还有什么建议我可以试试吗?阴影是否应该设置在cellForRowAtIndexPath方法之外的其他位置?我猜当你向上滚动时,它会以相反的顺序绘制单元格,这样每个单元格的阴影都会被它下面的单元格覆盖。也许您唯一的解决方案是向每个单元添加一个子视图,并在该子视图上放置阴影。调整所有内容的大小,使子视图及其阴影适合每个单元格,这样就不会有任何内容被另一个单元格剪裁。可能必须采用您建议的解决方案。如果这是唯一的办法,这不是很奇怪吗?在UI中有阴影是很常见的,但是在表视图中每个单元格都有阴影是很少见的。