2016-12-24 75 views
-1

我正在繪製時間軸視圖。我使用UITableView當我更新上一層底部繪製的圓形圖層的新數據。在UITableView中加載新數據時重疊圖層

這是加載新數據

overlapping layers when new data populating

這裏的時候,它的外觀是我的自定義單元代碼:

override func draw(_ rect: CGRect) { 
     if let anchor = anchorView() { 
      anchor.layer.zPosition = 3 
      let iconImageView = anchor as! UIImageView 
      iconImageView.image = iconImageView.image!.withRenderingMode(UIImageRenderingMode.alwaysTemplate) 
      iconImageView.tintColor = UIColor.white 

      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), 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 = 0 
      anchor.alpha = 1 
      //Set Anchor Z position 
      anchor.layer.zPosition = 2 
      shapeLayer.zPosition = 1 

      anchor.superview?.layer.insertSublayer(shapeLayer, at: 0 ) 
      // anchor.tintColor = UIColor.white 

      // Setting icon to layer 
      /* let imageLayer = CALayer() 
      imageLayer.contents = iconImageView.image 
      imageLayer.frame = anchor.frame 
      anchor.layer.addSublayer(imageLayer)*/ 
      // timelinePoint.draw(view: self.contentView) 
      } else { 
       print("this should not happen") 
     } 
    } 

我怎麼能解決這個問題

+0

設定框架 –

+0

Frame是不是一個問題。數據加載和單元高度發生變化時發生這種情況。所以錨點的位置發生了變化。那麼我如何刪除以前繪製的圖層? –

+0

你可以使用它的removefromsuperview。在設置響應之前,請移除圖層或嘗試viewToClear.layer.sublayers = nil; –

回答

0
itemImageView.layer.cornerRadius = 25; 

更新:由於用戶atulkhatri指出,如果不加它不會工作:

itemImageView.layer.masksToBounds = YES; 
+0

不起作用... –

0

打電話給你的圖像視圖在此擴展CAShapeLayer的

extension UIImageView { 
     public func maskCircle(anyImage: UIImage){ 
     self.contentMode = UIViewContentMode.ScaleAspectFill 
     self.layer.cornerRadius = self.frame.height/2 
     self.layer.masksToBounds = false 
     self.clipsToBounds = true 
     //make square(* must to make circle), resize(reduce the kilobyte) & fix rotation. 
     self.image = prepareImage(anyImage) 
     } 
    } 
+0

不起作用... –

相關問題