调整我的UIView的CAShapeLayer中心

问题描述 投票:0回答:1

我一直试图解决这个问题几天,没有一个建议解决了我的问题。我在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)
}

结果:enter image description here

ios cashapelayer
1个回答
0
投票

设置图层后,我怀疑单元格大小发生了变化。最简单的解决方法是在单元格的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
}
© www.soinside.com 2019 - 2024. All rights reserved.