Swift CAShapeLayer 画三角形

屏幕快照 2019-04-09 下午6.07.32.png
let triangleView = UIView(frame: CGRect(x: 50, y: 100, width: 200, height: 100))
triangleView.backgroundColor = .lightGray
self.view.addSubview(triangleView)
    
// 画三角
let trianglePath = UIBezierPath()
var point = CGPoint(x: 0, y: 100)
trianglePath.move(to: point)
point = CGPoint(x: 200/2, y: 0)
trianglePath.addLine(to: point)
point = CGPoint(x: 200, y: 100)
trianglePath.addLine(to: point)
trianglePath.close()
let triangleLayer = CAShapeLayer()
triangleLayer.path = trianglePath.cgPath
triangleLayer.fillColor = UIColor.green.cgColor
triangleView.layer.addSublayer(triangleLayer)

你可能感兴趣的:(Swift CAShapeLayer 画三角形)