我正在处理时间线 View 。我在图标中心画一条线,并用填充颜色画一个圆圈。但问题是,当我绘制圆圈时,它始终位于图标的顶部。现在图标不显示。我尝试了图层的 zposition 。这是我尝试过的
override func draw(_ rect: CGRect) {
if let anchor = anchorView(){
let centreRelativeToTableView = anchor.superview!.convert(anchor.center, to: self)
// print("Anchor x origin : \(anchor.frame.size.origin.x)")
timelinePoint.position = CGPoint(x: centreRelativeToTableView.x , y: centreRelativeToTableView.y/2)
timeline.start = CGPoint(x: centreRelativeToTableView.x , y: 0)
timeline.middle = CGPoint(x: timeline.start.x, y: anchor.frame.origin.y)
timeline.end = CGPoint(x: timeline.start.x, y: self.bounds.size.height)
timeline.draw(view: self.contentView)
let circlePath = UIBezierPath(arcCenter: CGPoint(x: anchor.center.x,y: anchor.center.y - 20), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
//change the fill color
shapeLayer.fillColor = UIColor.randomFlat.cgColor
// shapeLayer.fillColor = UIColor.clear.cgColor
//you can change the stroke color
shapeLayer.strokeColor = UIColor.white.cgColor
//you can change the line width
shapeLayer.lineWidth = 5
shapeLayer.zPosition = 0
anchor.alpha = 1
//Set Anchor Z position
anchor.layer.zPosition = 2
shapeLayer.zPosition = 1
anchor.layer.addSublayer(shapeLayer)
// Setting icon to layer
/*let imageLayer = CALayer()
imageLayer.contents = newImage
anchor.layer.addSublayer(imageLayer)*/
// timelinePoint.draw(view: self.contentView)
}else{
print("this should not happen")
}
}
我想在圆圈顶部绘制带有白色色调的图标。请帮助我
最佳答案
使用 addSublayer 将在顶部添加新层,这将覆盖之前添加的所有其他内容。
如果您知道您的图标位于哪一层,您可以使用 insertSublayer(at:) 将其放置在您的图标下方
或者,您可以使用 Storyboard 中图标的精确框架创建另一个 UIView,并将其放置在层次结构中的较低位置,以便您绘制的内容最终位于下面。
关于ios - swift中uiview的定位层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41157718/