我有一个标签,每隔5秒取一个不同的值(字符串值:“蓝色”,“粉红色”,“绿色”,“橙色”)
我想警告用户标签将在1秒前改变。 例如,在标签取另一个值之前,标签的背景颜色应为红色1秒。
每隔5秒调用颜色变化功能的代码
let timerFunc: NSTimer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: Selector("colorChange"), userInfo: nil, repeats: true)
随机更改标签值的代码
func colorChange() {
let minRandomValue: UInt = 0
let maxRandomValue: UInt = 4
var numberRandom: UInt = UInt()
numberRandom = UInt(arc4random()) % (maxRandomValue - minRandomValue)
switch numberRandom {
case 0: couleur.text = "Blue";
case 1: couleur.text = "Green";
case 2: couleur.text = "Orange";
case 3: couleur.text = "Pink";
default: println("value problem")
}
}
稍后我将使5秒定时器延迟随机。谢谢。 Xcode 6.4,SDK 8.0
答案 0 :(得分:1)
尝试以下方法......
1)创建一个SKNode, 2)创建一个红色的矩形SKSpriteNode并将其添加到SKNode, 3)创建一个SKLabelNode并将其添加到SKNode, 4)在self
上运行以下操作let changeColorWaitChangeBackground = SKAction.sequence([
SKAction.runBlock(self.changeColor),
SKAction.waitForDuration(4, withRange:2),
SKAction.runBlock(self.changeAlpha),
SKAction.waitForDuration(1),
])
在changeColor()中,设置颜色字符串并将红色SKSpriteNode的alpha设置为0.0。
在changeAlpha()中,将红色SKSpriteNode的alpha设置为1.0。
答案 1 :(得分:0)
将计时器间隔更改为1.0,以便每秒调用couleurChange
方法一次。在couleurChange
方法中,使用计数器跟踪时间。当计数器达到4时,更改标签的backgroundColor。当计数器达到5