2013-03-10 84 views
1

我已经添加阴影到一个UITableView(其覆盖屏幕sfrom的第三底部 - 见随附的屏幕截图),使用在一个UIView类别如下: CALayer的阴影而滚动的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

+0

它看起来像影子被应用于数百次的视图滚动 – CodaFi 2013-03-10 11:32:10

回答

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; }