【发布时间】:2021-08-31 15:16:59
【问题描述】:
我有一个向给定 UIView 添加进度视图的类。我在其单元格的集合视图中使用它。在前几个单元格上看起来不错,但在滑动几次后,您会看到应该存在的微弱标记。我将附上我的意思的截图。不知道为什么会这样?
这里是cellForItemAt
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cellId, for: indexPath) as? TakeCollectionViewCell else {return UICollectionViewCell()}
let service = TakeProgressView()
let random = Double(indexPath.row) / 10
service.progress = random
service.addView(view: cell.takeProgressView, withParticipants: false)
return cell
takeProgressView 是在 storyboard 中实现的 UIView
这是添加视图的函数:
func addView(view: UIView, withParticipants: Bool){
disagreePath.removeAllPoints()
agreePath.removeAllPoints()
path.removeAllPoints()
circlePath.removeAllPoints()
agreeLayer.removeFromSuperlayer()
disagreeLayer.removeFromSuperlayer()
backgroundFill.removeFromSuperlayer()
let width:CGFloat = 270
let height:CGFloat = 360
let viewX = (view.frame.size.width - width) / 2
let viewY = (view.frame.size.height - height) / 2
subView = UIView(frame: CGRect(x: viewX - 20, y: viewY, width: width + 40, height: height))
path = UIBezierPath(ovalIn: CGRect(x: 20, y: 0, width: width, height: height))
let rect = CGRect(x: 60, y: 40, width: width - 80, height: height - 80)
circlePath = UIBezierPath(ovalIn: rect)
path.append(circlePath)
path.usesEvenOddFillRule = true
backgroundFill = CAShapeLayer()
backgroundFill.path = path.cgPath
backgroundFill.lineWidth = 40.0
backgroundFill.fillRule = .evenOdd
backgroundFill.fillColor = #colorLiteral(red: 0.9371728301, green: 0.9373074174, blue: 0.9371433854, alpha: 1)
backgroundFill.opacity = 0.9
disagreePath = UIBezierPath(ovalIn: CGRect(x: 30, y: 10, width: width - 20, height: height - 20))
disagreeLayer = CAShapeLayer()
disagreeLayer.path = disagreePath.cgPath
disagreeLayer.fillColor = UIColor.clear.cgColor
disagreeLayer.strokeColor = #colorLiteral(red: 1, green: 0.4117892087, blue: 0.4117360711, alpha: 1)
disagreeLayer.strokeStart = 0.0
disagreeLayer.strokeEnd = CGFloat(1 - progress)
disagreeLayer.lineWidth = 20.0
disagreeLayer.lineCap = .round
backgroundFill.addSublayer(disagreeLayer)
agreePath = UIBezierPath(ovalIn: CGRect(x: 50, y: 30, width: width - 60, height: height - 60))
agreeLayer = CAShapeLayer()
agreeLayer.path = agreePath.cgPath
agreeLayer.fillColor = UIColor.clear.cgColor
agreeLayer.strokeColor = UIColor.uStadium.primary.cgColor
agreeLayer.strokeStart = CGFloat(1 - progress) + 0.02
agreeLayer.strokeEnd = 0.98
agreeLayer.lineWidth = 20.0
agreeLayer.lineCap = .round
backgroundFill.addSublayer(agreeLayer)
subView.layer.addSublayer(backgroundFill)
view.addSubview(subView)
}
【问题讨论】:
-
单元格被重复使用,所以每次添加子视图时...所以要么保留一个,要么删除之前添加的...
-
对,但是删除图层还不够吗?在函数的顶部
-
问题是你每次都在做
view.addSubview(subView)。所以每次,你都在上面添加一个新的TakeProgressView()。另外,所有路径都应该是空的,因为您每次都在创建一个TakeProgressView的新实例......脏修复?cell.view.forEach{ ($0 as? TakeProgressView)?.removeFromSuperview() } -
该视图还有一个我不想删除的标签
-
即使那个脏修复也行不通,因为子视图是
UIView而不是TakeProgressView。这很不清楚你为什么使用真正使用TakeProgressView。
标签: ios swift uibezierpath cashapelayer