2017-07-08 54 views
2

有很多這方面的文章,但無論出於什麼原因,我似乎無法得到正確的轉換正確的序列。我試圖讓UISliders直接進入SKLabels下面。轉換UIKit和SpriteKit之間的CGPoints困境:SKNodes旁邊的UIView

正如你可以在圖片中看到,滑塊的一個不顯示在所有的,另一種是在完全錯誤的地方(音量滑塊是尋求標籤附近):

enter image description here

這是我在得到這個恰當的電流的嘗試,雖然我已經試過這讓我無處一些其他配置:

class GameScene: SKScene { 

    let seekSlider = UISlider(frame: CGRect(x: 0, y: 0, width: 200, height: 15)) 
    let volSlider = UISlider(frame: CGRect(x: 0, y: 0, width: 200, height: 15)) 

    override func didMove(to view: SKView) { 
    removeAllChildren() // Delete this in your actual project. 

    // Add some labels: 
    let seekLabel = SKLabelNode(text: "Seek") 
    seekLabel.setScale(3) 
    seekLabel.verticalAlignmentMode = .center 
    seekLabel.position = CGPoint(x: frame.minX + seekLabel.frame.width/2, 
           y: frame.maxY - seekLabel.frame.height) 

    let volLabel = SKLabelNode(text: "Volume") 
    volLabel.setScale(3) 
    volLabel.verticalAlignmentMode = .center 
    volLabel.position = CGPoint(x: frame.minX + volLabel.frame.width/2, 
           y: frame.minY + volLabel.frame.height + volSlider.frame.height) 

    /* CONVERSION WOES BELOW: */ 

    // Configure sliders: 
    let seekOrigin = convertPoint(toView: convert(seekLabel.position, from: self)) 
    seekSlider.frame = CGRect(origin: seekOrigin, size: CGSize(width: 200, height: 15)) 
    seekSlider.value = 1 

    let volOrigin = convertPoint(fromView: CGPoint(x: volLabel.frame.minX, y: volLabel.frame.minY)) 
    seekSlider.frame = CGRect(origin: volOrigin, size: CGSize(width: 200, height: 15)) 
    seekSlider.value = 0 

    // Scene stuff: 
    view.addSubview(seekSlider) 
    view.addSubview(volSlider) 
    addChild(seekLabel) 
    addChild(volLabel) 
    } 
} 
+0

你試圖達到什麼樣的佈局?滑塊是否應該放在每個標籤下方? – nathan

+1

@nathan是的,雖然在這一點上,只是有正確的標籤附近的滑塊將是一個很好的開始:] – Fluidity

+0

HELL,我甚至將兩個滑塊_being在該死的screen_上作爲一個很好的答案:D – Fluidity

回答

3

你非常接近!有在你的代碼的兩個小問題:

問題#1

您修改seekSlider的框架,而不是修改seekSlider那麼volSlider的兩倍。

let volOrigin = convertPoint(fromView: CGPoint(x: volLabel.frame.minX, y: volLabel.frame.minY)) 
seekSlider.frame = CGRect(origin: volOrigin, size: CGSize(width: 200, height: 15)) 
seekSlider.value = 0 

應該是

let volOrigin = convertPoint(fromView: CGPoint(x: volLabel.frame.minX, y: volLabel.frame.minY)) 
volSlider.frame = CGRect(origin: volOrigin, size: CGSize(width: 200, height: 15)) 
volSlider.value = 0 

問題#2

轉換代碼是不正確的。

使用convertPoint(toView方法與節點的position應該做的伎倆:

let seekOrigin = convertPoint(toView: seekLabel.position) 

終極密碼:

class GameScene: SKScene { 

    let seekSlider = UISlider(frame: CGRect(x: 0, y: 0, width: 200, height: 15)) 
    let volSlider = UISlider(frame: CGRect(x: 0, y: 0, width: 200, height: 15)) 

    override func didMove(to view: SKView) { 
     removeAllChildren() // Delete this in your actual project. 

     // Add some labels: 
     let seekLabel = SKLabelNode(text: "Seek") 
     seekLabel.setScale(3) 
     seekLabel.verticalAlignmentMode = .center 
     seekLabel.position = CGPoint(x: frame.minX + seekLabel.frame.width/2, 
            y: frame.maxY - seekLabel.frame.height) 

     let volLabel = SKLabelNode(text: "Volume") 
     volLabel.setScale(3) 
     volLabel.verticalAlignmentMode = .center 
     volLabel.position = CGPoint(x: frame.minX + volLabel.frame.width/2, 
            y: frame.minY + volLabel.frame.height + volSlider.frame.height) 

     // Configure sliders: 
     let seekOrigin = convertPoint(toView: seekLabel.position) 
     let offsetSeekOrigin = CGPoint(x: seekOrigin.x, y: seekOrigin.y + 50) 
     seekSlider.frame = CGRect(origin: offsetSeekOrigin, size: CGSize(width: 200, height: 15)) 
     seekSlider.value = 1 

     let volOrigin = convertPoint(toView: volLabel.position) 
     let offsetVolOrigin = CGPoint(x: volOrigin.x, y: volOrigin.y - 50) 
     volSlider.frame = CGRect(origin: offsetVolOrigin, size: CGSize(width: 200, height: 15)) 
     volSlider.value = 0 

     // Scene stuff: 
     view.addSubview(seekSlider) 
     view.addSubview(volSlider) 
     addChild(seekLabel) 
     addChild(volLabel) 
    } 
} 

最終結果:

screenshot of sliders

+0

謝謝!!你能否詳細說明#1,但我不明白 – Fluidity

+0

我剛剛更新了我的答案。您可能複製/粘貼了一些代碼,但忘記更新變量名稱。 – nathan

+0

xD XD XD arghhhh – Fluidity

相關問題