平滑的 UIBezierPath

问题描述 投票:0回答:1

我在操场上创建了一个 uibezierpath 手动添加的值,它工作正常。结果是: enter image description here

现在我的目标是平滑曲线并去除这些“尖”角。我正在考虑插值函数,但不确定。下面是我的实际代码:

//: Playground - noun: a place where people can play

import Foundation
import UIKit
import CoreGraphics
import QuartzCore

class DemoView: UIView {


    override func draw(_ rect: CGRect) {
        let origin = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
        let radius = frame.size.width / 2

//        self.createCircle(origin: origin, radius: radius)
        self.addLinesInCircle(origin: origin, radius: radius)
    }

    func createCircle(origin: CGPoint, radius: CGFloat) {
        let path = UIBezierPath()
        path.addArc(withCenter: origin, radius: radius, startAngle: 0, endAngle: CGFloat(2 * Double.pi), clockwise: true)
        path.close()
        UIColor.clear.setFill()
        path.fill()
    }

    func addLinesInCircle(origin: CGPoint, radius: CGFloat) {
        let bezier = UIBezierPath()

        let incrementAngle: CGFloat = CGFloat.pi / 24
        let ratios: [CGFloat] = [3/6, 5/6, 3/6, 1/6, 5/6, 2/6, 4/6, 2/6, 4/6, 4/6, 4/6, 4/6,
                                 3/6, 5/6, 3/6, 1/6, 5/6, 2/6, 4/6, 2/6, 4/6, 4/6, 4/6, 4/6,
                                 3/6, 5/6, 3/6, 1/6, 5/6, 2/6, 4/6, 2/6, 4/6, 4/6, 4/6, 4/6,
                                 3/6, 5/6, 3/6, 1/6, 5/6, 2/6, 4/6, 2/6, 4/6, 4/6, 4/6, 4/6]

        for (index, ratio) in ratios.enumerated() {
            let point = CGPoint(x: origin.x + cos(CGFloat(index) * incrementAngle) * radius * ratio,
                                y: origin.y + sin(CGFloat(index) * incrementAngle) * radius * ratio)
            if index == 0 {
                bezier.move(to: point)
            } else {
                bezier.addLine(to: point)
            }
        }

        bezier.close()


        let gradient = CGGradient(colorsSpace: nil, colors: [UIColor.red.cgColor,UIColor.blue.cgColor,UIColor.yellow.cgColor ,UIColor.black.cgColor] as CFArray, locations: nil)!

        let ctx = UIGraphicsGetCurrentContext()!
             ctx.saveGState()

        // Clip to the path
        bezier.addClip()
        // Draw the gradient in the clipped region
        ctx.drawLinearGradient(gradient, start: CGPoint(x: 0, y: 0), end: CGPoint(x: 0, y: frame.height), options: [])

        ctx.restoreGState()

    }

}

let demoView = DemoView(frame: CGRect(x: 0, y: 0, width: 1000, height: 1000))

是否有人有一个想法或只是关键词以便寻找正确的方向?在此先感谢您的帮助。我希望我的解释足够了......

swift core-graphics interpolation uibezierpath swift-playground
1个回答
7
投票

使用二次曲线插值如下:

func MidPoint( _ l: CGPoint, _ r: CGPoint ) -> CGPoint { 
    return CGPoint( x: ( l.x + r.x ) / 2, y: ( l.y + r.y ) / 2 )
}

var start = CGPoint( x: 0, y: 0 )   
var prev = CGPoint( x: 0, y: 0 )

for (index, ratio) in ratios.enumerated() {
    let point = CGPoint(
        x: origin.x + cos(CGFloat(index) * incrementAngle) * radius * ratio,
        y: origin.y + sin(CGFloat(index) * incrementAngle) * radius * ratio
    )

    switch index {
    case  0:
        start = point
    case  1:
        bezier.move(
            to: MidPoint(start, point)
        )
        prev = point
    default:
        bezier.addQuadCurve(
            to: MidPoint(prev, point),
            controlPoint: prev
        )
        prev = point
    }
}

bezier.addQuadCurve(
    to: MidPoint(prev, start),
    controlPoint: prev
)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.