2017-05-30 102 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]; 
}