【发布时间】:2021-04-18 03:16:33
【问题描述】:
如何绘制如下图的背景路径?
我正在尝试自己,但安全不能正常来
在我的代码下面:
public class ShapeView : UIView{
public override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext()!
let corneRadius : CGFloat = 40.0
//// Color Declarations
let fillColor = UIColor(red: 0.125, green: 0.259, blue: 0.443, alpha: 1.000)
let fillColor2 = UIColor(red: 0.153, green: 0.314, blue: 0.525, alpha: 1.000)
//// g10
//// g12
//// path14 Drawing
context.saveGState()
context.translateBy(x: rect.origin.x, y: rect.origin.y)
// context.rotate(by: 0.8 * CGFloat.pi/180)
let path14Path = UIBezierPath(roundedRect: CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width, height: rect.size.height), cornerRadius: CGFloat(corneRadius))
fillColor.setFill()
path14Path.fill()
context.restoreGState()
//// g16
//// g18
//// g24
//// path30 Drawing
//// path32 Drawing
// let path32Path = UIBezierPath()
// path32Path.move(to: CGPoint(x: 0, y:0))
//// path32Path.addLine(to: CGPoint(x: 5.15, y: corneRadius))
//// path32Path.addCurve(to: CGPoint(x: corneRadius + 5 , y: 0), controlPoint1: CGPoint(x: corneRadius + 5 , y: corneRadius ), controlPoint2: CGPoint.zero)
//// path32Path.addCurve(to: CGPoint(x: 50.59, y: 5.01), controlPoint1: CGPoint(x: 50.53, y: 5.01), controlPoint2: CGPoint(x: 50.56, y: 5.01))
// path32Path.addLine(to: CGPoint(x: rect.width * 0.3, y: 0)) // top left x and y position of first shape
// path32Path.addLine(to: CGPoint(x: 0, y: rect.height * 0.5))
// path32Path.close()
// fillColor2.setFill()
// path32Path.fill()
// let path14Path = UIBezierPath(roundedRect: CGRect(x: -321.25, y: -190.1, width: 642.5, height: 380.2), cornerRadius: 44.6)
// let path30Path = UIBezierPath()
// path30Path.move(to: CGPoint(x: 182.31, y: 385.45))
// path30Path.addLine(to: CGPoint(x: 45.15, y: 385.16))
// path30Path.addCurve(to: CGPoint(x: 17.03, y: 374.93), controlPoint1: CGPoint(x: 34.43, y: 385.13), controlPoint2: CGPoint(x: 24.65, y: 381.31))
// path30Path.addLine(to: CGPoint(x: 329.68, y: 5.61))
// path30Path.addLine(to: CGPoint(x: 503.55, y: 5.99))
// path30Path.addLine(to: CGPoint(x: 182.31, y: 385.45))
// path30Path.close()
// fillColor2.setFill()
// path30Path.fill()
let path30Path = UIBezierPath()
path30Path.move(to: CGPoint(x: rect.width * 0.5, y: rect.height - 10))
path30Path.addLine(to: CGPoint(x: 10, y: rect.height - 10 ))
// path30Path.addCurve(to: CGPoint(x: corneRadius , y: 374.93), controlPoint1: CGPoint(x: rect.width * 0.5 * 2, y: 385.13), controlPoint2: CGPoint(x: rect.width * 0.5, y: 381.31))
path30Path.addLine(to: CGPoint(x: rect.width * 0.8, y: 0))
path30Path.addLine(to: CGPoint(x: rect.width * 0.35, y: rect.height))
path30Path.addLine(to: CGPoint(x: rect.width * 0.5, y: rect.height ))
path30Path.close()
fillColor2.setFill()
path30Path.fill()
//// path34 Drawing
// let path34Path = UIBezierPath()
// path34Path.move(to: CGPoint(x: 283.03, y: 385.67))
// path34Path.addLine(to: CGPoint(x: 208.2, y: 385.51))
// path34Path.addLine(to: CGPoint(x: 529.44, y: 6.04))
// path34Path.addLine(to: CGPoint(x: 603.49, y: 6.2))
// path34Path.addCurve(to: CGPoint(x: 604.26, y: 6.21), controlPoint1: CGPoint(x: 603.75, y: 6.2), controlPoint2: CGPoint(x: 604.01, y: 6.21))
// path34Path.addLine(to: CGPoint(x: 283.03, y: 385.67))
// path34Path.close()
// fillColor2.setFill()
// path34Path.fill()
//
//
// //// path36 Drawing
// let path36Path = UIBezierPath()
// path36Path.move(to: CGPoint(x: 522.68, y: 386.19))
// path36Path.addLine(to: CGPoint(x: 447.85, y: 386.03))
// path36Path.addLine(to: CGPoint(x: 646.21, y: 151.71))
// path36Path.addLine(to: CGPoint(x: 644.92, y: 241.79))
// path36Path.addLine(to: CGPoint(x: 522.68, y: 386.19))
// path36Path.close()
// fillColor2.setFill()
// path36Path.fill()
}
}
我想画那些背景浅色、形状。
输出:
【问题讨论】:
-
你为什么不用
CAGradientLayer? -
CAGradientLayer 和 Beizerpath 有什么区别?视图高度将根据屏幕高度 * 0.3 改变