美文网首页swift
swift Timer 定时器

swift Timer 定时器

作者: kindom_0129 | 来源:发表于2021-08-30 16:32 被阅读0次

    方式1

    var timer = Timer.scheduledTimer(timeInterval: TimeInterval(curTimeInterval), target: self, selector:#selector(self.xxx), userInfo: nil, repeats: true)
    

    方式2

    var timer = Timer(timeInterval: TimeInterval(curTimeInterval), target: self, selector: #selector(self.animate), userInfo: nil, repeats: true)
    RunLoop.current.add(timer, forMode: .common)
    timer?.fire()
    

    这种定时器的创建需要使用 fire 来启动定时器。否则,该定时器不起作用。如果定时器不添加到 RunLoop 中,在重复模式下定时器只执行一次。

    • 设置runloop mode
       RunLoop.current.add(timer, forMode: .common)
    
    • 取消定时器timer
    if timer != nil {
                timer?.invalidate();
                timer = nil;
            }
    

    相关文章

      网友评论

        本文标题:swift Timer 定时器

        本文链接:https://www.haomeiwen.com/subject/yqroiltx.html