gpt4 book ai didi

swift - 将 CGFloat 数组标准化为 UIView 框架高度

转载 作者:行者123 更新时间:2023-11-30 11:33:03 26 4
gpt4 key购买 nike

我正在 UIBezierPath 中绘制 CGFloat 值数组。现在我想对这些值进行标准化,以便数组的最大值将占据整个高度。

到目前为止

let scaleFactor = (waveView.frame.height) * readFile.maxValue

for point in readFile.points {
let nextPoint = CGPoint(x: soundPath.currentPoint.x + sampleSpace, y: middleY - (point * scaleFactor) - 1.0)

soundPath.addLine(to: nextPoint)
soundPath.move(to: nextPoint)
}

但是好像不行...

编辑读取文件:

class ReadFile {
public enum ReadFileError:Error{
case errorReading(String)

}

/* Player Interval Measured in Miliseconds (By now..) */
var beginPosition:Int32 = 0
var endPosition:Int32 = 0

/* Sample Rate -> Default 48KHz */
var sampleRate:Double = 48000

var samplesSeconds:CGFloat = 5

var maxValue:CGFloat = 0
var points:[CGFloat] = []

}

还有

sampleSpace = 0.2

最佳答案

谢谢安迪的回答,但我终于弄清楚了。

我正在绘制声波,因此它具有正值和负值。

heightMax = waveView.frame.height/2

应用三规则(西类牙语翻译)我最终得到这样的结果:

    func drawSoundWave(windowLength:Int32){

// Drawing code
print("\(logClassName): Drawing!!!")
print("\(logClassName): points COUNT = \(readFile.points.count)")

let soundPath = UIBezierPath()
soundPath.lineWidth = lineWidth
soundPath.move(to: CGPoint(x:0.0 , y: middleY))

print("\(logClassName) max ")
for point in readFile.points{

let normalizedHeight:CGFloat = (waveView.frame.height * point) / (2 * readFile.maxValue)
let nextPoint = CGPoint(x: soundPath.currentPoint.x + sampleSpace, y: middleY - (normalizedHeight))

soundPath.addLine(to: nextPoint)
soundPath.move(to: nextPoint)

}

let trackLayer = CAShapeLayer()
trackLayer.path = soundPath.cgPath

waveView.layer.addSublayer(trackLayer)

trackLayer.strokeColor = UIColor.red.cgColor
trackLayer.lineWidth = lineWidth
trackLayer.fillColor = UIColor.green.cgColor
trackLayer.lineCap = kCALineCapRound

}

哪里

let normalizedHeight:CGFloat = (waveView.frame.height * point) / (2 * readFile.maxValue) 

是给定 readFile.maxValue 和 waveView.frame.height 的归一化值

关于swift - 将 CGFloat 数组标准化为 UIView 框架高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50043899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com