Swift 倾斜矩形的一条边

Swift 倾斜矩形的一条边,swift,uibezierpath,Swift,Uibezierpath,我正试图在斯威夫特中倾斜一条尤比齐尔路径的边缘。我相信您可以通过在其中一条边上使用“移动”来实现这一点。与下文类似— let offset: CGFloat = 60.0; let path = UIBezierPath() let width = self.bounds.width - offset let upperLeftPoint = CGPoint(x: self.bounds.origin.x + width + offset, y: self.bounds.origin.y)

我正试图在斯威夫特中倾斜一条尤比齐尔路径的边缘。我相信您可以通过在其中一条边上使用“移动”来实现这一点。与下文类似—

let offset: CGFloat = 60.0;

let path = UIBezierPath()

let width = self.bounds.width - offset

let upperLeftPoint = CGPoint(x: self.bounds.origin.x + width + offset, y: self.bounds.origin.y)
let upperRightPoint = CGPoint(x: self.bounds.origin.x, y: self.bounds.origin.y)

let lowerRightPoint = CGPoint(x: self.bounds.origin.x, y: self.bounds.size.height)
let lowerLeftPoint = CGPoint(x: width - offset, 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)

// Close the path. This will create the last line automatically.
path.close()
UIColor.red.setFill()
path.fill()

let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
self.layer.mask = shapeLayer;
然而,这并不是我想要达到的目标。下面是我试图实现的目标。

我通过以下操作实现了此形状-

class Header: UIView {
    var path: UIBezierPath!

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.backgroundColor = UIColor.red
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    func createHeader() {
        // Drawing code
        // Get Height and Width
        let layerHeight = layer.frame.height
        let layerWidth = layer.frame.width
        // Create Path
        let bezierPath = UIBezierPath()
        //  Points
        let pointA = CGPoint(x: 0, y: 0)
        let pointB = CGPoint(x: layerWidth, y: 0)
        let pointC = CGPoint(x: layerWidth, y: layerHeight*2/3)
        let pointD = CGPoint(x: 0, y: layerHeight)
        // Draw the path
        bezierPath.move(to: pointA)
        bezierPath.addLine(to: pointB)
        bezierPath.addLine(to: pointC)
        bezierPath.addLine(to: pointD)
        bezierPath.close()
        // Mask to Path
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = bezierPath.cgPath
        layer.mask = shapeLayer
    }

    override func draw(_ rect: CGRect) {
        //    self.createRectangle()
        self.createHeader()
    }
}

也许这有帮助:对我来说很好。我在tableview单元格中使用了它