首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >光滑UIBezierPath

光滑UIBezierPath
EN

Stack Overflow用户
提问于 2018-06-12 08:06:19
回答 1查看 1.7K关注 0票数 1

我在操场上创建了一个由手工添加的uibezierpath,它工作得很好。结果是:

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

代码语言:javascript
复制
//: 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))

如果有人有一个想法或只是为了正确地看正确的方向而键入单词?提前谢谢你的帮助。我希望我的解释足够..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-17 02:40:07

使用二次曲线进行插值如下所示:

代码语言:javascript
复制
    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 )

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50812180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档