0

我正在研究时间线视图。我在图标的中心画了一条线,并用填充颜色画了一个圆圈。但问题是当我画圆圈时,它总是在图标的顶部。现在图标没有显示。我尝试了图层的 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")
            }


}

ss1-------- s2

我想在圆圈顶部绘制带有白色色调的图标。请帮我

4

1 回答 1

0

使用 addSublayer 将在顶部添加新层,它将覆盖之前添加的所有其他内容。

如果你知道你的图标在哪一层,你可以使用 insertSublayer(at:) 把它放在你的图标下面

或者,您可以使用故事板中图标的确切框架创建另一个 UIView,并将其放置在层次结构中的较低位置,这样无论您的绘图最终处于何种状态。

于 2016-12-15T06:32:06.193 回答