Гладкая UIBezierPath - PullRequest
       57

Гладкая UIBezierPath

0 голосов
/ 12 июня 2018

Я создал на детской площадке 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))

Если у кого-то есть идея или просто ключевые слова, чтобы смотреть в правильном направлении?Заранее спасибо за помощь.Я надеюсь, что мои объяснения достаточно ...

1 Ответ

0 голосов
/ 17 июля 2018

Интерполяция с использованием квадратичной кривой выглядит следующим образом:

    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

...