【问题标题】:How to make semi-transparent button with shadow?如何制作带阴影的半透明按钮?
【发布时间】:2015-10-09 14:13:37
【问题描述】:
我有一个 UIButton,它需要是白色和 50% 透明的。同时我需要影子。但是阴影是一个矩形,可以通过按钮背景看到。有没有可能用它做点什么?
我现在的代码:
self.WhiteBtn.backgroundColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:0.9];
self.WhiteBtn.opaque = NO;
self.WhiteBtn.clipsToBounds = NO;
self.WhiteBtn.layer.shadowColor = [UIColor blackColor].CGColor;
self.WhiteBtn.layer.shadowOpacity = 0.5;
self.WhiteBtn.layer.shadowRadius = 5;
self.WhiteBtn.layer.shadowOffset = CGSizeMake(0,0);
【问题讨论】:
标签:
ios
objective-c
core-graphics
shadow
【解决方案1】:
最初有两个问题:阴影的形状(最初是矩形)和按钮背景颜色与阴影颜色混合。此外,UIColor 初始化程序接受 [0.0, 1.0] 范围内的所有参数。最后,我做了更多的调整并得到了这个(参见下面的代码和示例按钮)。
代码:
let shadowPathWidth: CGFloat = 1.0 // fine tune your shadow's width with this
let layerAndShadowRadius: CGFloat = 5.0 //and its radius with this
WhiteBtn.backgroundColor = UIColor(red:1, green:1, blue:1, alpha:0.9)
WhiteBtn.layer.cornerRadius = layerAndShadowRadius
WhiteBtn.layer.shadowPath = CGPathCreateCopyByStrokingPath(CGPathCreateWithRoundedRect(WhiteBtn.bounds, layerAndShadowRadius, layerAndShadowRadius, nil), nil, shadowPathWidth, CGLineCap.Round, CGLineJoin.Bevel, 0.0)
WhiteBtn.layer.shadowOpacity = 1.0;
WhiteBtn.layer.shadowOffset = CGSizeMake(0,0)
示例按钮: