SACountingLabel is a highly customizable animated UILabel that can count, inspired by Tim Gostony's UICountingLabel. Originally created as an animated UI component for a game to display scores, SACountingLabel supports the use of various formats such as percentages, currencies and multiple decimal places. It can also be modified to use different animation modes such as linear, ease in, ease out as well as ease in and out.
SACountingLabel is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SACountingLabel"
And then run:
$ pod install
To manually install SACountingLabel, simply add SACountingLabel.swift
to your project.
To run the example project, clone the repo, and run pod install
from the Example directory first.
Creating a SACountingLabel is the same as creating a UILabel:
let label = SACountingLabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
Or add it to your storyboard or XIB file and set the class type to SACountingLabel
instead of UILabel
.
This is the general syntax for counting from a start value to an end value for a specified counting and animation style.
label.countFrom(0, to: 100, withDuration: 1.0, andAnimationType: .EaseIn, andCountingType: .Int)
The start value is a float
value that the counting begins from.
The end value is a float
value where the counting ends.
The duration is a float
value for the length of the animation in seconds.
There are four animation types - linear, ease in, ease out, as well as ease in and ease out.
Counts from the start value to the end value at a constant speed.
Starts counting from the start value slowly and speeds up as it approaches the end value.
Starts counting from the start value quickly and slows down as it approaches the end value.
Starts counting from the start value slowly, speeds up towards the middle, and slows down again as it approaches the end value.
There are three counting types - integer, floating point and custom.
This will result in counting by integers.
0, 1, 2,..., 100
This will result in counting using floating points, with a default of 2 decimal places.
0.00, 0.01, 0.02,..., 100
This allows you to specify a custom format for the counting. You will have to set the format like this:
label.format = "%.1f%%"
This will result in 0.0%, 0.1%, 0.2%,..., 100%
.
Formats like "$%.2f"
and "%i points"
can be used as well.
Copyright (c) 2015 Sudeep Agarwal and Tim Gostony
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.