最近流行一種設計叫彌散陰影,在用久了扁平之后找不到設計快感,又都開始嘗試微陰影的效果;所以必須要有個靚騷的通透的陰影來表現(xiàn)一些質(zhì)感,下面我用最簡單的方式來玩玩彌散陰影。

btn.png

QQ20160704-3.png
這就是最簡單的方式了,我這個設計小白也只會這個(囧); 當然更靚騷的方式還可以調(diào)整色階、亮度、曲線,加上模糊等效果。
但這不是重點,重點是怎么用代碼實現(xiàn)這個效果呢?模擬器中有一個是圖片,猜哪個是代碼實現(xiàn)的:

QQ20160704-5.png
上面一個是代碼,下面是圖片。
實現(xiàn)代碼:
#define kShadowRadius 20
@implementation MyShadowButton
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
[self initCommon];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initCommon];
}
return self;
}
- (void)initCommon {
UIColor *clr = [UIColor colorWithRed:81.0/255.0 green:224.0/255.0 blue:172.0/255.0 alpha:1.0];
self.backgroundColor = clr;
self.layer.shadowColor = clr.CGColor;
self.layer.cornerRadius = 5;
self.layer.shadowOffset = CGSizeMake(0,0);
self.layer.shadowOpacity = 1.0;
self.layer.shadowRadius = kShadowRadius;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(kShadowRadius, self.bounds.size.height/2, self.bounds.size.width-(2*kShadowRadius), self.bounds.size.height/2)].CGPath;
}
@end
如果陰影位置不一樣只要調(diào)一下shadowPath 就好了。