2017-05-30 103 views
4

我在单元格中有2个子视图。一个用于阴影视图,另一个用于角落半径,因为这听起来是一个很好的方法,我从this link找到。所以我试图用IBDessignables在Objective-C中做到这一点。一切工作正常,如果单元格大小相同,但动态大小的阴影显示在另一个单元格上,但角视图很好。带有IBDesignables的单元格中的拐角半径的阴影

- 细胞的景观层次 -

-- UITableViewCell 
    -- contentView 
    -- shadowView 
     -- CornerView 

这里是我的影子查看代码

ShadowView.h

IB_DESIGNABLE 
@interface ShadowView : UIView 
@property (nonatomic) IBInspectable CGFloat cornerRadius; 
@property (nonatomic) IBInspectable CGFloat shadowRadius; 
@property (nonatomic) IBInspectable CGSize shadowOffset; 
@end 

ShadowView.m

@implementation ShadowView 

-(void)setup{ 
    self.cornerRadius = 5.0f; 
    self.shadowRadius = 2.0f; 
    self.shadowOffset = CGSizeMake(0, 0); 
    self.backgroundColor = [UIColor clearColor]; 
} 

- (instancetype)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self setup]; 
    } 
    return self; 
} 


- (id)initWithCoder:(NSCoder *)aDecoder { 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
     [self setup]; 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect { 
    [self updateLayerProperties]; 
} 

- (void)updateLayerProperties { 
    self.layer.shadowOffset = self.shadowOffset; 
    self.layer.shadowRadius = self.shadowRadius; 
    self.layer.shadowOpacity = 0.3; 
    self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.cornerRadius].CGPath; 
    self.layer.masksToBounds = NO; 
    self.layer.shouldRasterize = YES; 
    self.layer.rasterizationScale = [UIScreen mainScreen].scale; 
} 

@end 

那么有谁知道确切的问题是什么?由于角视图边界完美地工作,但不是阴影视图。

演示文件

这里是演示文件,如果有人想测试一下.. https://www.dropbox.com/s/myxjyj5pu3ey3aw/demoDesignables.zip?dl=0

+0

所以你的意思是你的问题是阴影覆盖下面的单元格? – Suragch

+0

yes .. cornerview与阴影视图的子视图大小相同..但它的精细和阴影覆盖了另一个用于动态单元格高度的单元格 –

+0

我不太明白你在做什么。如果这些单元之间有更多的间距会修复它?显示你想要的图像会有所帮助。 – Suragch

回答

1

视图是负责绘制内容,处理多点触控事件,并管理所有子视图的布局。 因此,您需要覆盖ShadowView的功能layoutSubviews

-(void)layoutSubviews { 
    [super layoutSubviews]; 
    [self updateLayerProperties]; 
}