[英]CollectionViewCell Draw Rect
我正在尝试在我的集合视图单元格中绘制一个简单的轮廓圆圈。 由于某种原因,只有第一个单元格正在绘制,其余单元格没有显示。
class UserCell: UICollectionViewCell {
override func draw(_ rect: CGRect) {
let center = CGPoint(x: self.center.x - 1, y: 41)
let circularPath = UIBezierPath()
circularPath.addArc(withCenter: center, radius: 36, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)
UIColor.red.setStroke()
circularPath.lineWidth = 2
circularPath.stroke()
}
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.white
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我在这里错过了什么?
使用UICollectionViewDataSource方法cellForItemAt()将集合视图单元格配置为显示。 这些单元格将被重用,并且不会为每个“新”单元格自动重绘。 而不是覆盖draw(rect),将子视图添加到单元格并在cellForItemAt()中配置子视图。
创建了一个符合UIView()的新类,在其draw函数中添加了bezierPath信息。 然后在collectionView单元格中继承此类。 按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.