这是我视图的代码。
override func draw(_ rect: CGRect) {
let path = self.getPath()
path.stroke()
}
func getPath() -> UIBezierPath {
let path = UIBezierPath()
path.lineWidth = 1
path.lineCapStyle = .round
path.lineJoinStyle = .round
mainColor.setStroke()
mainColor.setFill()
path.move(to: CGPoint(x:0,y:0))
path.addQuadCurve(to: endPointLeft, controlPoint: controlPointLeft)
path.addLine(to: startPointRight)
path.addQuadCurve(to: endPointRight, controlPoint: controlPointRight)
path.addLine(to: startPointLeft)
path.fill()
path.stroke()
return path
}
工作正常。但是有时候进入背景一段时间后,这种观点就会消失。
*****更多信息*****
ViewController中的功能具有滚动功能,可更改此视图的高度。
更改高度限制后,调用self.view.layoutIfNeeded()
。
答案 0 :(得分:0)
您可以做的是在应用程序变为活动状态时重新布局子视图,如下所示,在控制器中首先添加观察者
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(_:)), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
}
@objc func applicationDidBecomeActive(_ notification:Notification)
{
self.view.layoutIfNeeded()
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
}
答案 1 :(得分:0)
原因:
在BG中呆了很长时间后,它将调用draw()函数重绘View。 就像视图的高度为0一样,它什么也不会画。
解决方案:
使用CAShapeLayer更新路径。
@IBInspectable var mainColor: UIColor = UIColor.blue
private var bottomLayer : CAShapeLayer = CAShapeLayer()
override func awakeFromNib() {
super.awakeFromNib()
bottomLayer = CAShapeLayer()
bottomLayer.strokeColor = mainColor.cgColor
bottomLayer.fillColor = mainColor.cgColor
bottomLayer.lineWidth = 1
bottomLayer.backgroundColor = UIColor.clear.cgColor
bottomLayer.path = self.getPath().cgPath
self.layer.addSublayer(bottomLayer)
}
override func layoutSubviews() {
super.layoutSubviews()
self.bottomLayer.frame = self.bounds
self.bottomLayer.path = self.getPath().cgPath
}