如何在 swift 中创建这样的视图?
how to create a view like this shape in swift?
如何创建此 2D 外观
如何创建这个。
非常感谢。
试试这个
class TwoDView : UIView {
public var xDiff : CGFloat = 5.0
public var yDiff : CGFloat = 5.0
override func draw(_ rect: CGRect) {
let ctx = UIGraphicsGetCurrentContext()
ctx?.move(to: CGPoint(x: 0, y: rect.height - yDiff))
ctx?.addLines(between: [
CGPoint(x: rect.width - xDiff, y: rect.height - yDiff),
CGPoint(x: rect.width, y: rect.height),
CGPoint(x: xDiff, y: rect.height),
CGPoint(x: 0, y: rect.height - yDiff)
])
ctx?.setFillColor(UIColor.black.cgColor)
ctx?.fillPath()
ctx?.move(to: CGPoint(x: rect.width - xDiff, y: 0))
ctx?.addLines(between: [
CGPoint(x: rect.width, y: yDiff),
CGPoint(x: rect.width, y: rect.height),
CGPoint(x: rect.width - xDiff, y: rect.height - yDiff),
CGPoint(x: rect.width - xDiff, y: 0)
])
ctx?.setFillColor(UIColor.gray.cgColor)
ctx?.fillPath()
UIImage(named: "Image")?.draw(in: CGRect(x: 0, y: 0, width: rect.width - xDiff, height: rect.height - yDiff))
}
}
如何创建此 2D 外观
如何创建这个。
非常感谢。
试试这个
class TwoDView : UIView {
public var xDiff : CGFloat = 5.0
public var yDiff : CGFloat = 5.0
override func draw(_ rect: CGRect) {
let ctx = UIGraphicsGetCurrentContext()
ctx?.move(to: CGPoint(x: 0, y: rect.height - yDiff))
ctx?.addLines(between: [
CGPoint(x: rect.width - xDiff, y: rect.height - yDiff),
CGPoint(x: rect.width, y: rect.height),
CGPoint(x: xDiff, y: rect.height),
CGPoint(x: 0, y: rect.height - yDiff)
])
ctx?.setFillColor(UIColor.black.cgColor)
ctx?.fillPath()
ctx?.move(to: CGPoint(x: rect.width - xDiff, y: 0))
ctx?.addLines(between: [
CGPoint(x: rect.width, y: yDiff),
CGPoint(x: rect.width, y: rect.height),
CGPoint(x: rect.width - xDiff, y: rect.height - yDiff),
CGPoint(x: rect.width - xDiff, y: 0)
])
ctx?.setFillColor(UIColor.gray.cgColor)
ctx?.fillPath()
UIImage(named: "Image")?.draw(in: CGRect(x: 0, y: 0, width: rect.width - xDiff, height: rect.height - yDiff))
}
}