Skip to content
项目

关于

简洁优雅的计时器

开发工具

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
0
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

platforms

Simple and Elegant Timer

中文介绍:打造一个优雅的Timer

Compare with NSTimer

  • No retain cycle
  • Decouple with RunLoop
  • Support GCD queue
  • Support dynamically changing interval
  • Support closure syntax

Usage

single timer

//We need to maintain a strong reference to SwiftTimer. When self dealloc, timer will dealloc too. 
self.timer = SwiftTimer(interval: .seconds(2)) {
    print("fire")
}
self.timer.start()

repeatic timer

timer = SwiftTimer.repeaticTimer(interval: .seconds(1)) {
    print("fire")
}
timer.start()

dynamically changing interval

timer = SwiftTimer.repeaticTimer(interval: .seconds(5)) { timer in
	print("doSomething")
}
timer.start()  // print doSomething every 5 seconds

func speedUp(timer: SwiftTimer) {
	timer.rescheduleRepeating(interval: .seconds(1))
}
speedUp(timer) // print doSomething every 1 second 

throttle

The closure will be called after interval you specified. It is invalid to call again in the interval.

SwiftTimer.throttle(interval: .seconds(0.5), identifier: "refresh") {
	refresh();
}

debounce

The closure will be called after interval you specified. Calling again in the interval cancels the previous call.

SwiftTimer.debounce(interval: .seconds(0.5), identifier: "search") {
	search(inputText)
}

count down timer

timer = SwiftCountDownTimer(interval: .fromSeconds(0.1), times: 10) { timer , leftTimes in
    label.text = "\(leftTimes)"
}
timer.start()

Installation

Swift Package Manager

SwiftTimer can be added as a Swift Package Manager dependency with Swift 5.0 or later. The package exposes the SwiftTimer library and keeps the existing deployment targets: iOS 8, macOS 10.11, tvOS 10, and watchOS 3. Your Xcode version may require a newer deployment target. The package also builds on Linux using Foundation and Dispatch.

In Xcode, choose File > Add Package Dependencies, enter https://github.com/100mango/SwiftTimer.git, and add the SwiftTimer product to your target. Select Up to Next Major Version starting at 1.2.0. Version 1.2.0 is the first release with a package manifest; the older v1.0 and 1.1 tags do not support Swift Package Manager.

For a Package.swift dependency:

dependencies: [
    .package(url: "https://github.com/100mango/SwiftTimer.git", from: "1.2.0")
],
targets: [
    .target(name: "YourTarget", dependencies: ["SwiftTimer"])
]

Then use import SwiftTimer in your Swift files. The package compiles in Swift 5 language mode, including when used with a Swift 6 toolchain.

Queue and cancellation behavior

Use each timer instance from a single serial queue. fire() calls its handler synchronously on the caller's queue; scheduled events use the queue passed to the initializer. Keep a strong reference to a timer for as long as it is needed.

Debounce and throttle registration and cancellation are safe to call concurrently. Identifiers share one registry across both operations. A pending throttle ignores the same identifier; debounce replaces any pending work with that identifier. The identifier becomes available before its handler runs, so handlers may schedule or cancel new work with the same identifier. Cancellation prevents pending work from starting; it does not interrupt a handler that has already started.

Carthage

github "100mango/SwiftTimer"

Testing

Run the cross-platform package tests with a current Swift toolchain:

swift test
swift test -c release

The package tests cover timers, countdowns, debounce, and throttle without UIKit, including lifecycle, reentrant handlers, concurrent cancellation, and input boundaries. The existing Xcode project and its iOS tests remain available for Apple-platform validation; Linux package tests do not replace Apple SDK or device testing.

探索相关项目

加载中…