如何在 Swift 中每隔 X 秒重复一次?
How to repeat every X seconds in Swift?
我正在尝试构建一个简单的单视图应用程序,该应用程序运行所选图像的无限幻灯片放映,每次图像更改之间都有时间延迟。
我为此编写的代码如下。我试图将它放入 viewDidLoad 和 viewDidAppear 但屏幕仍然空白,我猜这是因为该函数由于无限循环而永远不会完成。
我在 iOS 之前学习了一些 Python 并且使用 tkinter,您的代码将进入主循环。但我不太确定如何在 Swift
中做同样的事情
有人可以在 Swift 中解释为什么我遇到这个问题以及如何解决这个问题。谢谢
var arrayimages: [UIImage] = [UIImage(named: "charizard")!,UIImage(named:"Flying_Iron_Man")!]
var x: Int = 0
var images: UIImage
let arraycount = arrayimages.count
repeat{
images = arrayimages[(x % arraycount)]
sleep(1)
slideshow.image = images
x++
} while true
注意:幻灯片是一个图像视图出口。
您正在寻找 NSTimer
let timer = NSTimer.scheduledTimerWithTimeInterval(
1.0, target: self, selector: Selector("doYourTask"),
userInfo: nil, repeats: true)
第一个参数是您希望计时器触发的频率,第二个参数是哪个对象将具有被调用的选择器,第三个是选择器名称,第四个是您要传递的任何额外信息作为计时器对象的参数,第五个是是否应该重复。
如果您想在以后的任何时候停止代码:
timer.invalidate()
创建一个重复的 NSTimer:
var timer = NSTimer.scheduledTimerWithTimeInterval(2.0,
target: self,
selector: "animateFunction:",
userInfo: nil,
repeats: true)
然后写一个函数animateFunction:
func animateFunction(timer: NSTimer)
{
//Display the next image in your array, or loop back to the beginning
}
编辑:为现代 Swift 版本更新:(>= Swift 5)
自从我发布此答案以来,情况发生了很大变化。 NSTimer 现在在 Swift 中被称为 Timer,scheduledTimer()
方法的语法也发生了变化。方法签名现在是 scheduledTimer(timeInterval:target:selector:userInfo:repeats:)
此外,您创建选择器的方式也发生了变化
所以电话会是
var timer = Timer.scheduledTimer(timeInterval: 2.0,
target: self,
selector: #selector(animateFunction(_:)),
userInfo: nil,
repeats: true)
animateFunction 可能如下所示:
func animateFunction(timer: Timer)
{
//Display the next image in your array, or loop back to the beginning
}
我正在尝试构建一个简单的单视图应用程序,该应用程序运行所选图像的无限幻灯片放映,每次图像更改之间都有时间延迟。
我为此编写的代码如下。我试图将它放入 viewDidLoad 和 viewDidAppear 但屏幕仍然空白,我猜这是因为该函数由于无限循环而永远不会完成。
我在 iOS 之前学习了一些 Python 并且使用 tkinter,您的代码将进入主循环。但我不太确定如何在 Swift
中做同样的事情有人可以在 Swift 中解释为什么我遇到这个问题以及如何解决这个问题。谢谢
var arrayimages: [UIImage] = [UIImage(named: "charizard")!,UIImage(named:"Flying_Iron_Man")!]
var x: Int = 0
var images: UIImage
let arraycount = arrayimages.count
repeat{
images = arrayimages[(x % arraycount)]
sleep(1)
slideshow.image = images
x++
} while true
注意:幻灯片是一个图像视图出口。
您正在寻找 NSTimer
let timer = NSTimer.scheduledTimerWithTimeInterval(
1.0, target: self, selector: Selector("doYourTask"),
userInfo: nil, repeats: true)
第一个参数是您希望计时器触发的频率,第二个参数是哪个对象将具有被调用的选择器,第三个是选择器名称,第四个是您要传递的任何额外信息作为计时器对象的参数,第五个是是否应该重复。
如果您想在以后的任何时候停止代码:
timer.invalidate()
创建一个重复的 NSTimer:
var timer = NSTimer.scheduledTimerWithTimeInterval(2.0,
target: self,
selector: "animateFunction:",
userInfo: nil,
repeats: true)
然后写一个函数animateFunction:
func animateFunction(timer: NSTimer)
{
//Display the next image in your array, or loop back to the beginning
}
编辑:为现代 Swift 版本更新:(>= Swift 5)
自从我发布此答案以来,情况发生了很大变化。 NSTimer 现在在 Swift 中被称为 Timer,scheduledTimer()
方法的语法也发生了变化。方法签名现在是 scheduledTimer(timeInterval:target:selector:userInfo:repeats:)
此外,您创建选择器的方式也发生了变化
所以电话会是
var timer = Timer.scheduledTimer(timeInterval: 2.0,
target: self,
selector: #selector(animateFunction(_:)),
userInfo: nil,
repeats: true)
animateFunction 可能如下所示:
func animateFunction(timer: Timer)
{
//Display the next image in your array, or loop back to the beginning
}