我试图复制你在音乐应用程序中流行的外观,在那里你会看到一系列的Sound Bars上下移动到音乐的节拍。我的问题是我无法让我的条形音箱保持其底部边框固定在屏幕底部并调整其高度。我的缩放算法是正确的,它的效果很好,但我的问题是我的条形音箱在屏幕上上下移动,而不是粘在底部,只是调整它的高度。这就是我所拥有的。
func setUpSoundBars(){
//I have this as a skspritenode because i need to adjust its Z position
//"SoundBars" is just a red background image sized to the iphone6 plus
soundbar = SKSpriteNode(imageNamed: "SoundBars")
self.addChild(soundbar!)
}
//updateBeatParticle() is being called constantly by CADisplayLink
func updateBeatParticle(){
//Algorithm to get scale according to music
//DO NOT WORRY ABOUT Below
//-------------------------------------------------------
if let audio = backgroundMusic?.backgroundAudio {
var scale: CGFloat = 0
if (audio.playing) { // Only do this if the audio is actually playing
audio.updateMeters() // Tell the audio player to update and fetch the latest readings
let channels = audio.numberOfChannels
var power: Float = 0
//Loop over each channel and add its average power
for i in 0..<channels {
power += audio.averagePowerForChannel(i)
}
power /= Float(channels) // This will give the average power across all the channels in decibels
// Convert power in decibels to a more appropriate percentage representation
scale = CGFloat(getIntensityFromPower(power))
scale -= 0.6
if scale < 0 {
scale = 0
}
scale *= 2
}
//----------------------------------------------------
//DO NOT WORRY ABOUT ABOVE
//This is where sound bar is adjusted to the and its size is changed
//to match the beat of the music
//WHERE I NEED HELP!
if let view = self.view {
let height: CGFloat = view.frame.height * scale
soundbar?.position = CGPoint(x: 0, y: view.frame.height - height)
soundbar?.size = CGSize(width: (self.view?.bounds.width)! * 0.20, height: height)
}
}
}
答案 0 :(得分:0)
另一种解决方法是修改 SKSpriteNode 的yScale
。
例如,如果您拍摄垂直条形图并设置:
soundbar.anchorPoint = CGPoint(x: 0, y: 0)
您可以简单地缩放y轴以修改高度,它不会移动位置。
soundbar.yScale = 2
在您的情况下,我确信这可以很好地与您现有的比例计算相关联。