在 iOS 中創建這樣的圖表的最佳方法是什么?

我的第一個想法是創建一個貝塞爾曲線路徑,然后添加一個具有不同位置的漸變層。但這行不通,因為檔案規定:
這些值必須單調遞增。
在我的圖表中情況并非如此。
關于實作這一目標的好方法的任何想法?
謝謝
uj5u.com熱心網友回復:
您可以通過使用 aCAGradientLayer作為圖表的背景,然后使用 aCAShapeLayer作為漸變圖層的蒙版來做到這一點。遮罩層將僅在繪制它的區域中顯示下方的層。
這個游樂場代碼使用隨機生成的資料給出了一個總體思路:
import UIKit
import PlaygroundSupport
let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
view.backgroundColor = .black
// Gradient for the chart colors
let gradient = CAGradientLayer()
gradient.colors = [
UIColor.red.cgColor,
UIColor.orange.cgColor,
UIColor.yellow.cgColor,
UIColor.green.cgColor
]
gradient.startPoint = CGPoint(x: 0.5, y: 0)
gradient.endPoint = CGPoint(x: 0.5, y: 1)
gradient.frame = view.bounds
view.layer.addSublayer(gradient)
// Random points
let graph = CAShapeLayer()
let path = CGMutablePath()
var y: CGFloat = 150
let points: [CGPoint] = stride(from: CGFloat.zero, to: 300, by: 2).map {
let change = CGFloat.random(in: -20...20)
var newY = y change
newY = max(10, newY)
newY = min(newY, 300)
y = newY
return CGPoint(x: $0, y: y)
}
path.addLines(between: points)
graph.path = path
graph.fillColor = nil
graph.strokeColor = UIColor.black.cgColor
graph.lineWidth = 4
graph.lineJoin = .round
graph.frame = view.bounds
// Only show the gradient where the line is
gradient.mask = graph
PlaygroundPage.current.liveView = view
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/344321.html
下一篇:使用truffle console連接到公共區塊鏈網路,出現undefined Error: Mnemonic invalid or undefined
