gpt4 book ai didi

swift : Anchor SKSpriteNode

转载 作者:行者123 更新时间:2023-11-30 13:28:25 25 4
gpt4 key购买 nike

我正在尝试复制音乐应用程序中流行的外观,您会看到一系列条形音箱随着音乐的节拍上下移动。我的问题是,我无法让条形音箱将其底部边框固定在屏幕底部并仅调整其高度。我的缩放算法是正确的并且效果很好,但我的问题是我的条形音箱在屏幕上上下移动,而不是粘在底部并仅调整其高度。这是我所拥有的。

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)
}

}
}

最佳答案

解决此问题的另一种方法是修改 SKSpriteNodeyScale

例如,如果您拍摄竖条图像并设置:

soundbar.anchorPoint = CGPoint(x: 0, y: 0)

您可以简单地缩放 y 轴来修改高度,并且它不会移动位置。

soundbar.yScale = 2

就您而言,我确信这可以很好地与您现有的规模计算联系起来。

关于 swift : Anchor SKSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36859595/

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