Swift 2.0 随机数定时器
Swift 2.0 Timer for a random number
我需要一个在应用程序启动时启动的计时器。计时器用于 arc4random,它给我一个从 1 到 10 的随机数,并根据该数字选择多个 if 语句之一。我还需要在给出随机数时重置计时器,以便 arc4random 可以给出一个新的随机数。我还没有想出如何实现定时器和 arc4random,但我在下面给出了一个 if 语句的例子。
示例:
if timer <= 9 {
print(A)
}
if timer <= 5 {
print(B)
}
if timer >= 4 {
print(C)
}
let timer = Int(arc4random_uniform(10) + 1)
if timer < 3 {
// code
} else if timer < 5 {
// code
} else {
// code
}
我不确定这是否是您要查找的内容,但听起来像是...
var timer = NSTimer()
func viewDidLoad() {
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "getRandomNumber", userInfo: nil, repeats: true)
}
func getrandomNumber(){
let randomNumber = Int(arc4random_uniform(10) + 1)
if randomNumber >= 9 {
print("...")
} else if randomNumber < 9 {
print("...")
}
timer.invalidate()
resetTimer()
}
func resetTimer() {
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "getRandomNumber", userInfo: nil, repeats: true)
}
当视图加载时,timer
开始,它将每 1.0 秒调用一次函数 getRandomNumber()
。 getRandomNumber()
生成一个 randomNumber
然后根据你打印的数字或相应地做任何你想做的事情然后在 if-else
语句之后你使计时器无效,然后调用一个名为 resetTimer
,这将重新开始。
随机数由Int(arc4random_uniform(10) + 1)
生成,其中10为上限,+1为起始索引。所以这将生成 10 到 1 之间的数字。如果你这样做,例如:Int9arc4random_uniform(20) + 2)
,它将生成 20 到 2 之间的数字。
var randomNumber = Double(arc4random_uniform(10) + 1)
var timer = NSTimer()
self.timer = NSTimer.scheduledTimerWithTimeInterval(randomNumber, target: self, selector: "AnyFunctionYouWant", userInfo: nil, repeats: true)
解释:
-
randomNumber = Double(arc4random_uniform(10) + 1)
创建一个新变量 a 将其值设置为 arc4random 函数的 return。您必须将其转换为 double 才能与 NSTimer class 一起使用
var timer = NSTimer()
使用默认初始化程序创建一个新的 NSTimer class 实例
self.timer = NSTimer.scheduledTimerWithTimeInterval(<strong>randomNumber</strong>, 目标:自身,选择器:<strong>"AnyFunctionYouWant" </strong>,用户信息:无,重复:真)
使用预定的计时器间隔初始化您之前创建的 NSTimer 变量,该间隔设置为您之前生成的随机数。 如果您没有将随机数转换为 double,它将无法工作,因为此方法的参数需要一个double 不是 int
我需要一个在应用程序启动时启动的计时器。计时器用于 arc4random,它给我一个从 1 到 10 的随机数,并根据该数字选择多个 if 语句之一。我还需要在给出随机数时重置计时器,以便 arc4random 可以给出一个新的随机数。我还没有想出如何实现定时器和 arc4random,但我在下面给出了一个 if 语句的例子。
示例:
if timer <= 9 {
print(A)
}
if timer <= 5 {
print(B)
}
if timer >= 4 {
print(C)
}
let timer = Int(arc4random_uniform(10) + 1)
if timer < 3 {
// code
} else if timer < 5 {
// code
} else {
// code
}
我不确定这是否是您要查找的内容,但听起来像是...
var timer = NSTimer()
func viewDidLoad() {
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "getRandomNumber", userInfo: nil, repeats: true)
}
func getrandomNumber(){
let randomNumber = Int(arc4random_uniform(10) + 1)
if randomNumber >= 9 {
print("...")
} else if randomNumber < 9 {
print("...")
}
timer.invalidate()
resetTimer()
}
func resetTimer() {
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "getRandomNumber", userInfo: nil, repeats: true)
}
当视图加载时,timer
开始,它将每 1.0 秒调用一次函数 getRandomNumber()
。 getRandomNumber()
生成一个 randomNumber
然后根据你打印的数字或相应地做任何你想做的事情然后在 if-else
语句之后你使计时器无效,然后调用一个名为 resetTimer
,这将重新开始。
随机数由Int(arc4random_uniform(10) + 1)
生成,其中10为上限,+1为起始索引。所以这将生成 10 到 1 之间的数字。如果你这样做,例如:Int9arc4random_uniform(20) + 2)
,它将生成 20 到 2 之间的数字。
var randomNumber = Double(arc4random_uniform(10) + 1)
var timer = NSTimer()
self.timer = NSTimer.scheduledTimerWithTimeInterval(randomNumber, target: self, selector: "AnyFunctionYouWant", userInfo: nil, repeats: true)
解释:
-
randomNumber = Double(arc4random_uniform(10) + 1)
创建一个新变量 a 将其值设置为 arc4random 函数的 return。您必须将其转换为 double 才能与 NSTimer class 一起使用 var timer = NSTimer()
使用默认初始化程序创建一个新的 NSTimer class 实例self.timer = NSTimer.scheduledTimerWithTimeInterval(<strong>randomNumber</strong>, 目标:自身,选择器:<strong>"AnyFunctionYouWant" </strong>,用户信息:无,重复:真)
使用预定的计时器间隔初始化您之前创建的 NSTimer 变量,该间隔设置为您之前生成的随机数。 如果您没有将随机数转换为 double,它将无法工作,因为此方法的参数需要一个double 不是 int