gpt4 book ai didi

swift - 倾斜矩形 UIBezierPath 的一条边

转载 作者:行者123 更新时间:2023-11-30 10:27:52 25 4
gpt4 key购买 nike

我正在尝试在 Swift 中倾斜 UIBezierPath 的一个边缘。我相信您可以通过在其中一个边缘上使用 move 来完成此操作。类似于下面的 -

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;

但这并不是我想要实现的目标。以下是我试图实现的目标。 desired image

最佳答案

我通过执行以下操作实现了这个形状 -

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()
}
}

关于swift - 倾斜矩形 UIBezierPath 的一条边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59758253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com