Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Swifty API for NSTimer

License

NotificationsYou must be signed in to change notification settings

radex/SwiftyTimer

Repository files navigation

PlatformsCI StatusCocoaPodsCarthage compatibleSwift version

Modern Swifty API forNSTimer

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

ReadSwifty APIs: NSTimer for more information about this project.

Usage

You can easily schedule repeating and non-repeating timers (repeats and delays) usingTimer.every andTimer.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.ms1.second2.5.seconds5.seconds10.minutes1.hour2.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, usenew(after:) andnew(every:):

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

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

Callstart() to schedule timers created usingnew. 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 aTimer argument in the closure you pass in:

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

Installation

Note: If you're running Swift 2, useSwiftyTimer 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 copySources/SwiftyTimer.swift to your Xcode project.

More like this

If you like SwiftyTimer, check outSwiftyUserDefaults, which applies the same swifty approach toNSUserDefaults.

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.

About

Swifty API for NSTimer

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors10


[8]ページ先頭

©2009-2025 Movatter.jp