Swift 怎么画一个平行四边形

// 画一个平行四边形
class CustomRectView: UIView {
    override func draw(_ rect: CGRect) {
        let offset: CGFloat = 60.0;
        let path = UIBezierPath()
        let width = self.bounds.width - offset
        
        let upperLeftPoint = CGPoint(x: self.bounds.origin.x + offset, y: self.bounds.origin.y)
        let upperRightPoint = CGPoint(x: self.bounds.origin.x + width, y: self.bounds.origin.y)
        let lowerRightPoint = CGPoint(x: width - offset, y: self.bounds.size.height)
        let lowerLeftPoint = CGPoint(x: self.bounds.origin.x, y: self.bounds.size.height)
        
        path.move(to: upperLeftPoint)
        path.addLine(to: upperRightPoint)
        path.addLine(to: lowerRightPoint)
        path.addLine(to: lowerLeftPoint)
        path.addLine(to: upperLeftPoint)
        
        path.lineWidth = 5
        // Close the path. This will create the last line automatically.
        path.close()
        UIColor.yellow .setFill()
        path.fill()
        
        // 注意目前以下代码,CustomRectView的背景颜色是可以修改的,但上一个改不了
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath
        shapeLayer.fillColor = UIColor.green.cgColor
        self.layer.addSublayer(shapeLayer)
    }
}

你可能感兴趣的:(Swift 怎么画一个平行四边形)