ios - 如何将多个贝塞尔曲线路径添加到同一 View swift 3

标签 ios swift3 uibezierpath

我这样添加了一个三角形

// draw the line graph

    UIColor.white.setFill()
    UIColor.white.setStroke()

    ///--------SETUP FIRST TRIANGLE-----------------------------------------------
    //set up the points line
    let graphPath = UIBezierPath()
    //go to start of line
    graphPath.move(to: CGPoint.init(x: rect.minX, y: rect.minY))
    graphPath.addLine(to: CGPoint.init(x: (rect.maxX/2.0), y: (rect.maxY/2.0)))
    graphPath.addLine(to: CGPoint.init(x: rect.minX, y: rect.maxY))
    graphPath.stroke()
    //2 - make a copy of the path
    let clippingPath = graphPath.copy() as! UIBezierPath

    //3 - add lines to the copied path to complete the clip area
    clippingPath.addLine(to: CGPoint.init(x: rect.minX, y: rect.minY))
    clippingPath.close()

    //4 - add the clipping path to the context
    clippingPath.addClip()

    let startPoint = CGPoint(x:(rect.maxX/2.0), y: (rect.maxY/2.0))
    let endPoint = CGPoint(x:rect.minX, y:(rect.maxY/2.0))
    let gradient=self.getGradient(tiangleNo: 1)
    let context = UIGraphicsGetCurrentContext()
    context!.drawLinearGradient(gradient, start: startPoint, end: endPoint, options: CGGradientDrawingOptions(rawValue: 0))

这是在画一个我想要的三角形。现在我想画另一个三角形。所以我喜欢这个。

///------------SECOND TRIANGLE -----------------------------------------------


    let graphPath2 = UIBezierPath()

    graphPath2.move(to: CGPoint.init(x: rect.minX, y: rect.minY))
    graphPath2.addLine(to: CGPoint.init(x: rect.maxX, y: rect.minY))
    graphPath2.addLine(to: CGPoint.init(x: (rect.maxX/2.0), y: (rect.minY/2.0)))
    graphPath2.stroke()

    let oldPath1=clippingPath.copy() as! UIBezierPath
    oldPath1.append(graphPath2)

    let startPoint2 = CGPoint(x:(rect.maxX/2.0), y: (rect.maxY/2.0))
    let endPoint2 = CGPoint(x:(rect.maxX/2.0), y:rect.minY)
    let gradient2=self.getGradient(tiangleNo: 2)
    let context2 = UIGraphicsGetCurrentContext()
    context2!.drawLinearGradient(gradient2, start: startPoint2, end: endPoint2, options: CGGradientDrawingOptions(rawValue: 0))

但这只是在前一个三角形内添加了一个三角形。rect是我的 UIScreen 矩形

最佳答案

简单的解决方案

你可以这样写一个函数

func drawTriangle(point1:CGPoint, point2:CGPoint, point3:CGPoint) {

    let path = UIBezierPath()
    path.move(to: point1)
    path.addLine(to: point2)
    path.addLine(to: point3)
    path.close()

    path.lineWidth = 2.0
    UIColor.red.setFill()
    path.fill()

}

调用函数绘制三角形

//Triagle 1

let pointA = CGPoint(x: 0, y: 0)
let pointB = CGPoint(x: 100, y: 0)
let pointC = CGPoint(x: 0, y: 100)

drawTriangle(point1: pointA, point2: pointB, point3: pointC)

//Triagle 2

let pointD = CGPoint(x: 120, y: 0)
let pointE = CGPoint(x: 220, y: 0)
let pointF = CGPoint(x: 220, y: 100)

drawTriangle(point1: pointD, point2: pointE, point3: pointF)

你会得到这样的东西,

enter image description here

关于ios - 如何将多个贝塞尔曲线路径添加到同一 View swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44327687/

相关文章:

ios - 在 View 中重新定位 CGPath/UIBezierPath

ios - 呈现后模糊 View Controller 变暗

iOS:使用 A5 芯片的设备上的解密方法崩溃

ios - 如何将一个 Storyboard中的导航 Controller 连接到另一个 Storyboard中的 View Controller ?

json - 如何使用 Core Data 保存 JSON 响应,在 Swift 3 中互联网离线时显示数据?

objective-c - 将箭头附加到 UIBezierPath

ios - 使用 PanGestureRecognizer 裁剪 UIImageView 内 UIImage 的右侧

ios - 在 Xcode 10 中使用启动图像时,iPhone XS Max/XR 无法使用原始分辨率

Swift 3 重构 : "Type ' NSView' has no member . .."错误

ios - 如何将 UIBezierPath 矩形的起点和终点更改为 CAShapeLayer