【发布时间】:2020-06-02 12:02:03
【问题描述】:
我试图向我的 UIButton 添加多个阴影。正如您在图像中看到的那样,添加了两个阴影。但是,它们隐藏了 UIButton 的标题和背景颜色。为什么会这样?那么,图层的顺序是不是变成了bottomLayer、topLayer、text、background?
实际结果
预期的结果
这就是我的 UIButton 类的外观。
class PrimaryButton: UIButton {
let cornerRadius: CGFloat = 10
override init(frame: CGRect) {
super.init(frame: frame)
}
convenience init() {
self.init(frame: .zero)
configure()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
addDropShadow()
}
private func configure() {
backgroundColor = .white;
layer.cornerRadius = cornerRadius
setTitle("Get Followers", for: .normal)
setTitleColor(Colours.buttonTextColour, for: .normal)
}
private func addDropShadow() {
let topLayer = createShadowLayer(color: Colours.shadowWhite, offset: CGSize(width: -6, height: -6))
let bottomLayer = createShadowLayer(color: Colours.shadowBlack, offset: CGSize(width: 6, height: 6))
layer.addSublayer(topLayer)
layer.addSublayer(bottomLayer)
}
private func createShadowLayer(color: UIColor, offset: CGSize) -> CALayer {
let shadowLayer = CALayer()
shadowLayer.masksToBounds = false
shadowLayer.shadowColor = color.cgColor
shadowLayer.shadowOpacity = 1
shadowLayer.shadowOffset = offset
shadowLayer.shadowRadius = 10
shadowLayer.shouldRasterize = true
shadowLayer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: 10).cgPath
return shadowLayer
}
}
【问题讨论】:
-
试试这个。它易于使用且看起来不错 - github.com/hirokimu/EMTNeumorphicView
-
您好感谢您推荐图书馆。但我想在不使用任何库的情况下尝试一下。
标签: swift uibutton swift4 swift5