2016-04-26 68 views
0

我試圖複製你在音樂應用程序中流行的外觀,你會看到一系列的聲吧上下移動到音樂節拍。我的問題是,我無法讓我的音吧保持其底部邊界固定在屏幕底部,並調整其高度。我的縮放算法是正確的,並且效果很好,但是我的問題是我的條形音棒在屏幕上下移動,而不是粘到底部而只是調整高度。這是我的。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) 
     } 

    } 
} 

回答

0

另一種方式來處理,這是修改SKSpriteNodeyScale

舉例來說,如果你把一個豎線形象,並設置:

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

您可以簡單地按比例y軸它來修改高度,它不會移動位置。

soundbar.yScale = 2 

在你的情況,我相信這可以很好地綁定到你現有的比例計算。