swift - 平滑的 UIBezierPath

标签 swift core-graphics interpolation uibezierpath swift-playground

我在 Playground 上创建了一个 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))

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

最佳答案

使用二次曲线插值如下:

    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

关于swift - 平滑的 UIBezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50812180/

相关文章:

swift - 使用 sqlite.swift 读/写数据库路径

ios - 是否可以更改 NavigationButton 以显示图像而不是蓝色?

ios - 适用于 iOS 的即时 alpha 或魔棒工具

python - numpy interp 减少 xp

ios - swift 4 中的“UIImage ?' is not convertible to ' UIImage”问题

ios - 如何使用 CoreGraphics 重新着色图像?

ios - 从 UIGraphicsGetImageFromCurrentImageContext 调用 CGImageSourceCreateWithDataProvider 时的 kCGImageStatusUnknownType

python - 具有 'linear' 和 'cubic' 的 Scipy griddata 产生 nan

c++ - 很难从 C++ boost 库中获取 barycentric_rational 以按照我需要的方式工作

ios - Dropbox 委托(delegate)方法不是第一次调用