swift - 将 CGFloat 数组标准化为 UIView 帧高度
问题描述
我正在绘制 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 的归一化值
推荐阅读
- javascript - 在谷歌表格外部库中使用 Java 脚本原型函数
- c# - 生成器 Firebird - 实体框架代码优先 .Net
- angular - 通过 RouterOutlet 更改 Angular 主题
- c# - 如何将 ThenInclude 实施到 EF Core 自定义规范中?
- android - 从三个位置的数组中找到离当前位置最近的位置,并比较哪个位置离当前位置最近
- android - 打开键盘而不是整个布局时向上移动一个特定视图
- c# - EF Core 奇怪的行为
- youtrack - 如何在 YouTrack 板上仅显示当前 sprint 的问题?
- uproot - 如何用锯齿状数组作为分支编写树?
- php - laravel 6 身份验证“npm run dev 不工作”