Ich versuche, einen populären Look zu reproduzieren, den Sie bei Musikanwendungen erhalten, wo Sie eine Reihe von Soundbars sehen, die sich im Takt der Musik bewegen. Mein Problem ist, dass ich meine Soundbar nicht dazu bringen kann, den unteren Rand am unteren Rand des Bildschirms zu verankern und einfach seine Höhe anzupassen. Mein Algorithmus für die Skalierung ist korrekt und es funktioniert gut, aber mein Problem ist, dass meine Soundbar auf dem Bildschirm auf und ab bewegt, anstatt auf dem Boden zu bleiben und nur seine Höhe anzupassen. Hier ist was ich habe.Swift: Anchor SKSpriteNode
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)
}
}
}