【问题标题】:scheduledTimer with two decimal带两位小数的定时定时器
【发布时间】:2020-06-15 14:28:55
【问题描述】:

是否可以使用带有两位小数的 schedulerTimer

让 timer = Timer.scheduledTimer(timeInterval: 0.10, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)

时间间隔为 0.10

【问题讨论】:

  • 是的,你可以。 TimeInterval 只是 Double 的类型别名。你可以使用你喜欢的小数位数(当然在某些时候它会变得非常不精确,但是使用两位小数就可以了)。
  • 不错不错。谢谢!

标签: ios swift uikit


【解决方案1】:

肯定的。

请注意,scheduedTimer 方法中的 timeInterval 参数描述为:

计时器触发之间的秒数。 如果ti 小于 大于或等于 0.0,此方法选择非负值 0.1 而是毫秒。

文档: https://developer.apple.com/documentation/foundation/timer/1412416-scheduledtimer

【讨论】:

    【解决方案2】:

    是的,您可以使用 TimeInterval 来实现,并将所需的值替换为 0.5

    let timer = Timer.scheduledTimer(timeInterval: TimeInterval(0.5), target: self, selector: #selector(//yourMethod), userInfo: nil, repeats: false)
    

    所以 Foundation 中的 scheduledTimer

    open class func scheduledTimer(timeInterval ti: TimeInterval, target aTarget: Any, selector aSelector: Selector, userInfo: Any?, repeats yesOrNo: Bool) -> Timer
    

    TimeInterval 在哪里

    public typealias TimeInterval = Double
    

    【讨论】:

    • 这如何回答这个问题?您建议的 0.5 值仍然只有一位小数;)
    • 不知道timeInterval是double的类型别名
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多