对于特定条件,我正在cellForItemAt方法中绘制一个圆,如下所示:

    if cellState.date == CalendarModel.DUE_DATE {
        let shapeLayer = CAShapeLayer()

        let circlePath = UIBezierPath(arcCenter: CGPoint(x: (cell.layer.frame.size.width)/2,y: (cell.layer.frame.size.height)/2), radius: CGFloat(15), startAngle: CGFloat(0), endAngle:CGFloat(Double.pi * 2), clockwise: true)


        shapeLayer.path = circlePath.cgPath

        //change the fill color
        shapeLayer.fillColor = UIColor.clear.cgColor
        //you can change the stroke color
        shapeLayer.strokeColor = UIColor.FlatColor.Blue.midnightBlue.cgColor
        //you can change the line width
        shapeLayer.lineWidth = 1.0
        cell.layer.addSublayer(shapeLayer)
    }


圆圈不断重复。我正在尝试willDisplayCell中的以下代码,但是它会删除单元格中的所有内容。

func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
    // comment
    cell.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
}


如何专门删除在shapeLayer中添加的cellForItemAt?任何帮助将不胜感激。谢谢。

最佳答案

按照@ Paulw11的建议,我将JTAppleCell子类化,并添加了一个变量,如下所示:

    var shapeLayer : CAShapeLayer!


然后在同一JTAppleCell的子类中添加另一个方法,如下所示:

func addCircle() {
    self.shapeLayer = CAShapeLayer()
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: (self.layer.frame.size.width)/2,y: (self.layer.frame.size.height)/2), radius: CGFloat(15), startAngle: CGFloat(0), endAngle:CGFloat(Double.pi * 2), clockwise: true)
    shapeLayer.path = circlePath.cgPath
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = UIColor.FlatColor.Blue.cgColor
    shapeLayer.lineWidth = 1.0
    self.layer.addSublayer(shapeLayer)
}


cellForItemAt中,在else块中使单元出队后,添加了以下几行:

if cell.shapeLayer != nil {
      cell.shapeLayer.removeFromSuperlayer()
      cell.shapeLayer = nil
}


谢谢@ Paulw11,以上解决方案对我有用。

07-27 16:39