Avinash kumawat
Avinash kumawat

Reputation: 75

Positioning layers of uiview in swift

I am working on a timeline view. I am drawing a line in the centre of my icon and drawing a circle with fill colour. But the problem is that when I draw the circle, it is always on the top of the icon. Now the icon is not showing. I tried zposition of the layers. Here is what I tried

    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

I want to draw my icon with white tint on top of the circle. Please help me

Upvotes: 0

Views: 2436

Answers (1)

Ben Ong
Ben Ong

Reputation: 931

Using addSublayer will add the new layer on top, which will cover everything else added before.

If you know which layer your icon is at, you can instead use insertSublayer(at:) to place it under your icon

Alternatively you can create another UIView with the exact frame of the icon in storyboard and place it lower in the hierarchy so whatever your draw ends up under.

Upvotes: 0

Related Questions