0

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?任何帮助将不胜感激。谢谢你。

4

1 回答 1

0

根据@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,上述解决方案对我有用。

于 2017-11-27T20:49:14.157 回答