2017-02-10 55 views
0

一些視覺上的錯誤我有以下細胞UITableView的佈局:與CALayer的

•UIView的

••的UIImageView

•••半透明疊加的UIView爲的UIImageView子視圖

也有模仿「播放」按鈕和UILabel的圖標視圖,但它們似乎與此錯誤無關。

而且我有以下代碼:

override func layoutSubviews() { 
     super.layoutSubviews() 

     createPath() 
    } 

    private func createPath() { 
     let padding = CGFloat(16) 
     let bottomLineLength = CGFloat(64) 
     let arcPadding = CGFloat(8) 

     let width = self.contentView.frame.width 
     let height = self.contentView.frame.height 

     if pathLayer != nil { 
      pathLayer?.removeFromSuperlayer() 
     } 

     pathLayer = CAShapeLayer() 

     let path = UIBezierPath() 

     path.move(to: CGPoint(x: padding + arcPadding, y: padding)) 
     path.addLine(to: CGPoint(x: width - padding - arcPadding, y: padding)) 

     // Top-Right arc 
     path.addArc(withCenter: CGPoint(x: width - padding - arcPadding, y: padding + arcPadding), 
        radius: arcPadding, 
        startAngle: CGFloat(3 * M_PI/2), 
        endAngle: CGFloat(M_PI * 2), 
        clockwise: true) 

     path.move(to: CGPoint(x: width - padding, y: padding + arcPadding)) 
     path.addLine(to: CGPoint(x: width - padding, y: height - padding - arcPadding)) 

     // Bottom-Right arc 
     path.addArc(withCenter: CGPoint(x: width - padding - arcPadding, y: height - padding - arcPadding), 
        radius: arcPadding, 
        startAngle: CGFloat(M_PI * 2), 
        endAngle: CGFloat(M_PI/2), 
        clockwise: true) 
     path.move(to: CGPoint(x: width - padding - arcPadding, y: height - padding)) 
     path.addLine(to: CGPoint(x: width - padding - bottomLineLength, y: height - padding)) 

     path.move(to: CGPoint(x: padding + bottomLineLength, y: height - padding)) 
     path.addLine(to: CGPoint(x: padding + arcPadding, y: height - padding)) 

     // Bottom-Left arc 
     path.addArc(withCenter: CGPoint(x: padding + arcPadding, y: height - padding - arcPadding), 
        radius: arcPadding, 
        startAngle: CGFloat(M_PI/2), 
        endAngle: CGFloat(M_PI), 
        clockwise: true) 

     path.move(to: CGPoint(x: padding, y: height - padding - arcPadding)) 
     path.addLine(to: CGPoint(x: padding, y: padding + arcPadding)) 

     // Top-Left arc 
     path.addArc(withCenter: CGPoint(x: padding + arcPadding, y: padding + arcPadding), 
        radius: arcPadding, 
        startAngle: CGFloat(M_PI), 
        endAngle: CGFloat(3 * M_PI/2), 
        clockwise: true) 

     pathLayer?.rasterizationScale = 2 * UIScreen.main.scale 
     pathLayer?.shouldRasterize = true 
     pathLayer?.strokeColor = UIColor(red: 0xFF/255, green: 0xFF/255, blue: 0xFF/255, alpha: 0.5).cgColor 
     pathLayer?.lineWidth = 2.0 
     pathLayer?.path = path.cgPath 

     contentView.layer.addSublayer(pathLayer!) 
    } 

我稱它在layoutSubviews對於重新創建的設備旋轉的路徑。

如果你看截圖,會看到一些黑色,並在路徑附近旋轉了一些黑色的「洞」。它們全都看起來一樣,並且它們當然不在UIImageView中的圖像上。

Screenshot

我認爲這是關係到我的CALayer。如何解決它?

+0

有一個沿着每個圓角矩形的內部左邊緣的黑色楔子。如果您放大,則可以看到。沿着頂部,右側和左下邊緣也有楔形。 –

+0

@robmayoff我很高興你看到他的意思! – matt

回答

2

你的問題是這些黑楔子:

black wedges

的根本原因是,CAShapeLayer的默認fillColor是不透明的黑色。將其設爲零,以避免填充:

pathLayer?.fillColor = nil 

做完這些,讓我告訴你一個較短的方式來構建你的路徑:

let cgPath = CGMutablePath() 
let start = CGPoint(x: padding + bottomLineLength, y: height - padding) 
let blCorner = CGPoint(x: padding, y: height - padding) 
let tlCorner = CGPoint(x: padding, y: padding) 
let trCorner = CGPoint(x: width - padding, y: padding) 
let brCorner = CGPoint(x: width - padding, y: height - padding) 
let end = CGPoint(x: width - padding - bottomLineLength, y: height - padding) 

cgPath.move(to: start) 
cgPath.addArc(tangent1End: blCorner, tangent2End: tlCorner, radius: arcPadding) 
cgPath.addArc(tangent1End: tlCorner, tangent2End: trCorner, radius: arcPadding) 
cgPath.addArc(tangent1End: trCorner, tangent2End: brCorner, radius: arcPadding) 
cgPath.addArc(tangent1End: brCorner, tangent2End: end, radius: arcPadding) 
cgPath.addLine(to: end) 

shapeLayer.path = cgPath 

結果:

no wedges