ios - swift:从数组中多次调用动作
问题描述
我有 int 值的数组:
var array = [1,82,17,29,74,94,76,97,48,78,88,20,31]
和行动:
@IBAction func action(_ sender: Any) {
print("i")
}
我想为数组中的值调用我的操作。
例如:第一次动作在 1 秒后调用,下一次在 82 秒后调用,下一次在 17 秒后调用。等等...
怎么做?
解决方案
使用下面的代码来实现这一点。
var count = 0
let seconds = array[count] // get initial value
_ = Timer.scheduledTimer(timeInterval: seconds, target: self, selector: #selector(callMethod), userInfo: nil, repeats: false)
@objc func callMethod() {
print("CallMethod....")
count += 1
if count < array.count {
let nextTime = array[count] // get next time value
_ = Timer.scheduledTimer(timeInterval: nextTime, target: self, selector: #selector(callMethod), userInfo: nil, repeats: false)
}
}
推荐阅读
- r - 如何查找特定日期的股票价格?
- javascript - 修复期间前后的字母,即使在不同的 div 内
- reactjs - 在 ReactJS 上,当 api 调用得到令牌过期错误时,自动重定向 Auth 页面的最佳方法是什么?
- vim - 如何全局删除VIM中尖括号之间的所有内容?
- javascript - 如何将数组映射到另一个对象数组
- bash - 运行 shell 脚本会启动新的 shell 而不是运行 shell 脚本
- javascript - 检查匹配的键是否相等
- html - 如何在 div 标签内向上移动 ap 标签?
- swift - 在 Swift 中初始化类的问题
- azure - 有没有办法在不打开路由的情况下连接到具有私有 IP 的 AKS 节点?