我一直试图解决这个问题几天,没有一个建议解决了我的问题。我在UIView
里面有一个UITableView
。我一直试图在我的CAShapeLayer
中绘制我的UIView
,但我无法做到正确。
我需要我的黄色CAShapeLayer
正好在灰色的UIView
中心。
func DrawActivityRect(cell: CalorieDashboardTVCell){
let rectangle = UIBezierPath(rect: cell.calorieBurnUIView.bounds)
let trackLayer = CAShapeLayer()
trackLayer.frame = cell.calorieBurnUIView.bounds
trackLayer.path = rectangle.cgPath
trackLayer.position = cell.calorieBurnUIView.center
trackLayer.strokeColor = UIColor.yellow.cgColor
trackLayer.lineWidth = 10
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.lineCap = kCALineCapRound
cell.calorieBurnUIView.layer.addSublayer(trackLayer)
}
设置图层后,我怀疑单元格大小发生了变化。最简单的解决方法是在单元格的layoutSubviews()
中。由于您的路径使用绝对坐标,因此您需要更新图层框架和路径。
override func layoutSubviews() {
super.layoutSubviews()
// You need to keep trackLayer around after you created it
let rectangle = UIBezierPath(rect: cell.calorieBurnUIView.bounds)
self.trackLayer.frame = cell.calorieBurnUIView.bounds
self.trackLayer.path = rectangle.cgPath
self.trackLayer.position = cell.calorieBurnUIView.center
}