2016-12-15 118 views
0

我正在研究時間軸視圖。我在我的圖標中心繪製了一條線,並用填充顏色繪製了一個圓。但問題是,當我繪製圓時,它總是在圖標的頂部。現在圖標沒有顯示。我嘗試了圖層的位置。以下是我試過快速定位uiview圖層

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

我想畫我的圖標與白色的色調在圓的上方。請幫助我

回答

0

使用addSublayer將在頂部添加新圖層,它將覆蓋之前添加的所有內容。

如果你知道你的圖標在哪一層,你可以改用insertSublayer(在:)將它放在圖標下

另外,您可以創建另一個UIView的與圖標的故事板和地點的確切框架它在層次結構中較低,因此無論您的繪製結果如何。