使用 UIBezierPath 向 UIView 添加阴影
Adding shadow to UIView with UIBezierPath
我正在 UIView 的 draw rect 方法中创建自定义形状。这是一个箭头并且可以正常工作,但是我在添加阴影时遇到了问题。我试图将阴影添加到 View 本身和图层,但它不起作用。任何帮助将不胜感激。
import UIKit
class ArrowView: UIView {
override func draw(_ rect: CGRect) {
//Drawing Arrow
let path = UIBezierPath()
let edge:CGFloat = 20.0
path.move(to: CGPoint(x: 0.0, y: 0.0))
path.addLine(to: CGPoint(x: self.frame.width - edge, y: 0.0))
path.addLine(to: CGPoint(x: self.frame.width , y: self.frame.height/2))
path.addLine(to: CGPoint(x: self.frame.width - edge , y: self.frame.height))
path.addLine(to: CGPoint(x: 0, y: self.frame.height))
path.close()
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
这里是获得影子的方法
import UIKit
@IBDesignable
class ArrowView: UIView {
private lazy var arrowLayer : CALayer = {
let layer = CALayer()
layer.backgroundColor = UIColor.red.cgColor
return layer
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupView()
}
func setupView() {
self.backgroundColor = .clear
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 1
layer.shadowOffset = .zero
layer.shadowRadius = 10
layer.addSublayer(arrowLayer)
}
private func updatePath() {
let path = UIBezierPath()
let edge:CGFloat = 20.0
path.move(to: CGPoint(x: 0.0, y: 0.0))
path.addLine(to: CGPoint(x: self.bounds.size.width - edge, y: 0.0))
path.addLine(to: CGPoint(x: self.bounds.size.width , y: self.bounds.size.height/2))
path.addLine(to: CGPoint(x: self.bounds.size.width - edge , y: self.bounds.size.height))
path.addLine(to: CGPoint(x: 0, y: self.bounds.size.height))
path.close()
arrowLayer.frame = self.bounds
let mask = CAShapeLayer()
mask.path = path.cgPath
arrowLayer.mask = mask
}
override func layoutSubviews() {
updatePath()
}
}
我正在 UIView 的 draw rect 方法中创建自定义形状。这是一个箭头并且可以正常工作,但是我在添加阴影时遇到了问题。我试图将阴影添加到 View 本身和图层,但它不起作用。任何帮助将不胜感激。
import UIKit
class ArrowView: UIView {
override func draw(_ rect: CGRect) {
//Drawing Arrow
let path = UIBezierPath()
let edge:CGFloat = 20.0
path.move(to: CGPoint(x: 0.0, y: 0.0))
path.addLine(to: CGPoint(x: self.frame.width - edge, y: 0.0))
path.addLine(to: CGPoint(x: self.frame.width , y: self.frame.height/2))
path.addLine(to: CGPoint(x: self.frame.width - edge , y: self.frame.height))
path.addLine(to: CGPoint(x: 0, y: self.frame.height))
path.close()
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
这里是获得影子的方法
import UIKit
@IBDesignable
class ArrowView: UIView {
private lazy var arrowLayer : CALayer = {
let layer = CALayer()
layer.backgroundColor = UIColor.red.cgColor
return layer
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupView()
}
func setupView() {
self.backgroundColor = .clear
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 1
layer.shadowOffset = .zero
layer.shadowRadius = 10
layer.addSublayer(arrowLayer)
}
private func updatePath() {
let path = UIBezierPath()
let edge:CGFloat = 20.0
path.move(to: CGPoint(x: 0.0, y: 0.0))
path.addLine(to: CGPoint(x: self.bounds.size.width - edge, y: 0.0))
path.addLine(to: CGPoint(x: self.bounds.size.width , y: self.bounds.size.height/2))
path.addLine(to: CGPoint(x: self.bounds.size.width - edge , y: self.bounds.size.height))
path.addLine(to: CGPoint(x: 0, y: self.bounds.size.height))
path.close()
arrowLayer.frame = self.bounds
let mask = CAShapeLayer()
mask.path = path.cgPath
arrowLayer.mask = mask
}
override func layoutSubviews() {
updatePath()
}
}