在swift中定位uiview的图层

时间:2016-12-15 06:22:42

标签: ios swift uiview draw cglayer

我正在制作时间轴视图。我在我的图标的中心画一条线并画一个带填充颜色的圆圈。但问题是,当我绘制圆圈时,它始终位于图标的顶部。现在图标没有显示。我试过了层的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

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

1 个答案:

答案 0 :(得分:0)

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

如果您知道图标所在的图层,则可以使用insertSublayer(at :)将其放在图标下

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