滚动UITableView时的CALayer阴影

时间:2013-03-10 11:08:52

标签: objective-c uitableview calayer shadow

我在UIView类别中使用以下内容为UITableView添加了一个阴影(它覆盖了底部三分之一的屏幕 - 参见附页截图):

- (void) addShadow {
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
    self.layer.masksToBounds = NO;
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOpacity = 1;
    self.layer.shadowOffset = CGSizeMake(-5,-5);
    self.layer.shadowRadius = 20;
    self.layer.shadowPath = path.CGPath;
    self.layer.shouldRasterize = YES;
}

它按预期显示,但当我向上滚动时,阴影也会向上滚动。此外,表格滚动超出其上限。你能说出这里有什么问题吗?如果我评论self.layer.masksToBounds = NO;,阴影消失,但表格滚动是预期的。因此,问题可能在masksToBounds左右。

Initial State

Scrolled state

1 个答案:

答案 0 :(得分:2)

我通过在下面放置一个相同的视图来解决它,只是为了阴影。不是一个干净的解决方案......因此我仍然愿意接受答案。我的代码如下:

- (UIView*) addShadow {
    UIView* backView = [[UIView alloc] initWithFrame:self.frame];
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:backView.bounds];
    backView.layer.masksToBounds = NO;
    backView.layer.shadowColor = [UIColor blackColor].CGColor;
    backView.layer.shadowOpacity = 1;
    backView.layer.shadowOffset = CGSizeMake(-5,-5);
    backView.layer.shadowRadius = 20;
    backView.layer.shadowPath = path.CGPath;
    backView.layer.shouldRasterize = YES;
    [self.superview addSubview:backView];
    [self.superview bringSubviewToFront:self];
    return backView;
}

  • (void) removeShadow { self.layer.masksToBounds = YES; self.layer.shadowColor = nil; self.layer.shadowOpacity = 0; self.layer.shadowOffset = CGSizeMake(0,0); self.layer.shadowRadius = 0; }