2017-07-31 79 views
0

enter image description here我正在尋找UIBezierPath和CAShapeLayer的幫助來繪製一個半環,並製作一個截圖視圖。使屏幕截圖細節頁面UI?

我做下面的代碼,但它畫完整的三角形。

let triangleLayer = CAShapeLayer() 
     let trianglePath = UIBezierPath() 
     trianglePath.move(to: .zero) 
     trianglePath.addLine(to: CGPoint(x: -size, y: up ? size : -size)) 
     trianglePath.addLine(to: CGPoint(x: size, y: up ? size : -size)) 
     trianglePath.close() 
     triangleLayer.path = trianglePath.cgPath 
     triangleLayer.fillColor = UIColor.red.cgColor 
     triangleLayer.anchorPoint = .zero[![enter image description here][1]][1] 
     triangleLayer.position = CGPoint(x: x, y: y) 
     subview.layer.addSublayer(triangleLayer) 

回答

1

它可以是非常有幫助到您的路徑上給名點:

enter image description here

subview.backgroundColor = .blue 

let triangleLayer = CAShapeLayer() 
let trianglePath = UIBezierPath() 

let w = subview.bounds.size.width 
let h = subview.bounds.size.height 

// move to Top-Left corner 
trianglePath.move(to: .zero) 

// line to Top-Right corner 
trianglePath.addLine(to: CGPoint(x: w, y: 0)) 

// line to Right-Side, 75% of height 
trianglePath.addLine(to: CGPoint(x: w, y: h * 0.75)) 

// line to Bottom-Left corner 
trianglePath.addLine(to: CGPoint(x: 0, y: h)) 

trianglePath.close() 

triangleLayer.path = trianglePath.cgPath 
triangleLayer.fillColor = UIColor.red.cgColor 
triangleLayer.anchorPoint = .zero 

subview.layer.addSublayer(triangleLayer) 

會導致:

enter image description here