avatar

UIKit-Shadow and CornerRadius

to use both effects

self.contentView.layer.cornerRadius = 5
self.contentView.layer.masksToBounds = true
self.contentView.addShadow(.black, sRadius: 1, sOpacity: 0.3, offset: (1, 1))

and addShadow in layoutSubviews function

func addShadow(_ sColor: UIColor, sRadius: CGFloat, sOpacity: Float, offset: (CGFloat, CGFloat), for changedRect: CGRect = .zero) {
let rect = changedRect == .zero ? self.bounds : changedRect
self.layer.shadowColor = sColor.cgColor
self.layer.shadowRadius = sRadius
self.layer.shadowOpacity = sOpacity
// 设置 shadowOffset 会产生离屏渲染
// self.layer.shadowOffset = CGSize(width: 5, height: 5)
let path = UIBezierPath(roundedRect: rect.offsetBy(dx: offset.0, dy: offset.1), cornerRadius: self.layer.cornerRadius)
self.layer.shadowPath = path.cgPath
self.layer.masksToBounds = false
}
文章作者: X Mεl0n
文章链接: http://www.zrzz.site/2020/09/29/UIKit-Shadow-and-CornerRadius/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 X Mεl0n | 随手记

评论