GitXplorerGitXplorer
r

SwiftyTimer

public
1237 stars
138 forks
10 issues

Commits

List of commits on branch master.
Verified
87ef0edd97333264c5fbc2bd3097840f7a7307a1

Merge pull request #45 from asowers1/as/Swift4.2

aasowers1 committed 6 years ago
Unverified
b12a05e877a493e8fde4fe4c1cbb54a191f417e3

Added changelog, updated README and Podspec to reflect Swift 4.2 version

aasowers1 committed 6 years ago
Unverified
d8db0304f7957f03d5b88a5b5e237537c5b06d7c

using proper Apple TV simulator name for tvOS 12

aasowers1 committed 6 years ago
Unverified
20536d7e25fb55db428f9bd2353a55ee8d3050f3

using locatable tvOS simulator for travis.yml

aasowers1 committed 6 years ago
Unverified
7618afc2ed319ce0fb833dc42602ba229eab290b

updated Swift version file, bumped podspec to 2.1.0, removed swift lint from travis test due to some issue with Xcode 10 and cocoapods

aasowers1 committed 6 years ago
Unverified
4cb937629c6feb5865ac73425930b188c5e425dc

remove cocoapods from cache

aasowers1 committed 6 years ago

README

The README file for this repository.

SwiftyTimer

Platforms CI Status CocoaPods Carthage compatible Swift version

Modern Swifty API for NSTimer

SwiftyTimer allows you to instantly schedule delays and repeating timers using convenient closure syntax. It's time to get rid of Objective-C cruft.

Read Swifty APIs: NSTimer for more information about this project.

Usage

You can easily schedule repeating and non-repeating timers (repeats and delays) using Timer.every and Timer.after:

Timer.every(0.7.seconds) {
    statusItem.blink()
}

Timer.after(1.minute) {
    println("Are you still here?")
}

You can specify time intervals with these intuitive helpers:

100.ms
1.second
2.5.seconds
5.seconds
10.minutes
1.hour
2.days

You can pass method references instead of closures:

Timer.every(30.seconds, align)

Manual scheduling

If you want to make a timer object without scheduling, use new(after:) and new(every:):

let timer = Timer.new(every: 1.second) {
    println(self.status)
}

(This should be defined as an initializer, but a bug in Foundation prevents this)

Call start() to schedule timers created using new. You can optionally pass the run loop and run loop modes:

timer.start()
timer.start(modes: .defaultRunLoopMode, .eventTrackingRunLoopMode)

Invalidation

If you want to invalidate a repeating timer on some condition, you can take a Timer argument in the closure you pass in:

Timer.every(5.seconds) { (timer: Timer) in
    // do something
    
    if finished {
        timer.invalidate()
    }
}

Installation

Note: If you're running Swift 2, use SwiftyTimer v1.4.1

CocoaPods

If you're using CocoaPods, just add this line to your Podfile:

pod 'SwiftyTimer'

Install by running this command in your terminal:

pod install

Then import the library in all files where you use it:

import SwiftyTimer

Carthage

Just add to your Cartfile:

github "radex/SwiftyTimer"

Manually

Simply copy Sources/SwiftyTimer.swift to your Xcode project.

More like this

If you like SwiftyTimer, check out SwiftyUserDefaults, which applies the same swifty approach to NSUserDefaults.

You might also be interested in my blog posts which explain the design process behind those libraries:

Contributing

If you have comments, complaints or ideas for improvements, feel free to open an issue or a pull request.

Author and license

Radek Pietruszewski

SwiftyTimer is available under the MIT license. See the LICENSE file for more info.