标签在Swift中消失

时间:2015-01-28 15:08:05

标签: ios swift label

我在spriteKit中的场景中有2个标签和2个滑块

当我触摸滑块时,它上方的标签消失了,我不明白为什么,

import SpriteKit
var bg = SKSpriteNode()

class Settings: SKScene {

let button   = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
let statButton   = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
let speedLabel = SKLabelNode()
let impulseLabel = SKLabelNode()
var sliderDemo = UISlider()
var gravityDemo = UISlider()
var speedLabelValue = SKLabelNode()
var impulseLabelValue = SKLabelNode()
override func didMoveToView(view: SKView) {
  backgroundImage()


    let height = CGFloat(view.bounds.height/2)
    let width = CGFloat(view.bounds.width/2)



// Change Bird Speed
    speedLabel.fontSize = 30
    speedLabel.text = "Change Speed:"
    speedLabel.position = CGPoint(x: width, y: height*2-60)
    addChild(speedLabel)

    sliderDemo.frame = CGRectMake(width-125, 33, 250, 100)
    sliderDemo.minimumValue = 1
    sliderDemo.maximumValue = 7
    sliderDemo.continuous = true
    sliderDemo.tintColor = UIColor.whiteColor()
    sliderDemo.value = 3.0
    sliderDemo.addTarget(self, action: "speedSlider:", forControlEvents: .ValueChanged)
    view.addSubview(sliderDemo)



// Change Bird Impulse (Gravity)
    impulseLabel.fontSize = 30
    impulseLabel.text = "Change Gravity:"
    impulseLabel.position = CGPoint(x: width, y: height*2-150)
    addChild(impulseLabel)

    gravityDemo.frame = CGRectMake(width-125, 120, 250, 100)
    gravityDemo.minimumValue = 60
    gravityDemo.maximumValue = gravMax
    gravityDemo.continuous = true
    gravityDemo.tintColor = UIColor.whiteColor()
    gravityDemo.value = gravMid
    gravityDemo.addTarget(self, action: "gravitySlider:", forControlEvents: .ValueChanged)
    view.addSubview(gravityDemo)








    let image2 = UIImage(named: "arrowright") as UIImage!
    button.frame = CGRectMake(width*1.5-37.5, height*1.51, 75, 75)
    button.setTitle("Play Game", forState: UIControlState.Normal)
    button.setImage(image2, forState: .Normal)
    button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
    button.addTarget(self, action: "GameSceneFunc", forControlEvents: UIControlEvents.TouchUpInside)
    button.alpha = 1
    view.addSubview(self.button)



    // Stat Button
    let image = UIImage(named: "stats") as UIImage!
    statButton.frame = CGRectMake(width/2-37.5, height*1.5, 75, 75)
    statButton.setTitle("View Statistics...", forState: UIControlState.Normal)
    statButton.setImage(image, forState: .Normal)
    statButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
    statButton.addTarget(self, action: "Statistics", forControlEvents: UIControlEvents.TouchUpInside)
    statButton.alpha = 1
    view.addSubview(self.statButton)



}









func speedSlider(sender:UISlider!)
{
    var sValue = Int32(sender.value)
    speedLabelValue.text = String(sValue)


    if (sValue == 1) {
        sValue = 7
    } else if (sValue == 2) {
        sValue = 6
    } else if (sValue == 3) {
        sValue = 5
    } else if (sValue == 4) {
        sValue = 4
    } else if (sValue == 5) {
        sValue = 3
    } else if (sValue == 6) {
        sValue = 2
    } else if (sValue == 7) {
        sValue = 1
    }

    dur1 = Double(sValue)+0.5
    dur2 = Double(sValue)+0.5


}







func gravitySlider(sender:UISlider!) {
    var sValue2 = Int32(sender.value)
    impulseLabelValue.text = String(sValue2)


    impulse = CGFloat(sValue2)


}

只是为了澄清 - 当我在去到场景后点击滑块时,它上方的标签将消失,当我触摸另一个滑块时,标签不会消失,无论我触摸的是什么,首先链接到它的标签是改变的那个

1 个答案:

答案 0 :(得分:3)

在您的代码段中,我没有看到您添加speedLabelValue

speedLabelValue.fontSize = 30
speedLabelValue.fontColor = UIColor.whiteColor()
speedLabelValue.text = "v"
speedLabelValue.position = CGPoint(x: width, y: height*2-120)
addChild(speedLabelValue)

我也没有看到您添加impulseLabelValue

impulseLabelValue.fontSize = 30
impulseLabelValue.text = "g"
impulseLabelValue.position = CGPoint(x: width, y: height*2-210)
addChild(impulseLabelValue)

但是,在我的情况下,当我添加这两个标签时,它更新了。

我还通过手动调用.ValueChanged方法初始化这两个标签:

speedSlider(sliderDemo)
gravitySlider(gravityDemo)