GithubHelp home page GithubHelp logo

nikolaisa / efcountinglabel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from efprefix/efcountinglabel

1.0 1.0 0.0 457 KB

Adds animated counting support to UILabel.

Home Page: http://cocoapods.org/pods/EFCountingLabel

License: MIT License

Ruby 4.16% Swift 91.59% Objective-C 4.25%

efcountinglabel's Introduction

A label which can show number change animated, in Swift.

Overview

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Version Needs
1.x XCode 8.0+
Swift 3.0+
iOS 8.0+
4.x XCode 9.0+
Swift 4.0+
iOS 8.0+

Installation

EFCountingLabel is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'EFCountingLabel'

Setup

Simply initialize a EFCountingLabel the same way you set up a regular UILabel:

let myLabel = EFCountingLabel(frame: CGRect(x: 10, y: 10, width: 200, height: 40))
self.view.addSubview(myLabel)

You can also add it to your xib or storyboard , just make sure you set the class and module to EFCountingLabel.

Use

Set the format of your label. This will be filled with a single int or float (depending on how you format it) when it updates:

myLabel.format = "%d"

Alternatively, you can provide a formatBlock, which permits greater control over how the text is formatted:

myLabel.formatBlock = {
      (value) in
      return "Score: " + (formatter.string(from: NSNumber(value: Int(value))) ?? "")
}

There is also a attributedFormatBlock to use an attributed string. If the formatBlock is specified, it takes precedence over the format.

Optionally, set the mode. The default is EFLabelCountingMethod.linear, which will not change speed until it reaches the end. Other options are described below in the Methods section.

myLabel.method = .easeOut

When you want the label to start counting, just call:

myLabel.countFrom(5, to: 100)

You can also specify the duration. The default is 2.0 seconds.

myLabel.countFrom(1, to: 10, withDuration: 3.0)

Additionally, there is animationDuration property which you can use to override the default animation duration.

myLabel.animationDuration = 1.0

You can use common convinient methods for counting, such as:

myLabel.countFromCurrentValueTo(100)
myLabel.countFromZeroTo(100)

Behind the scenes, these convinient methods use one base method, which has the following full signature:

myLabel.countFrom(
      startValue: CGFloat,
      to: CGFloat,
      withDuration: TimeInterval
)

You can get current value of your label using currentValue method (works correctly in the process of animation too):

let currentValue = myLabel.currentValue()

Optionally, you can specify a completionBlock to perform an acton when the label has finished counting:

myLabel.completionBlock = {
      () in
      print("finish")
}

Formats

When you set the format property, the label will look for the presence of %(.*)d or %(.*)i, and if found, will cast the value to Int before formatting the string. Otherwise, it will format it using a CGFloat.

If you're using a CGFloat value, it's recommended to limit the number of digits with a format string, such as "%.1f" for one decimal place.

Because it uses the standard String(format: String, arguments: CVarArg...) method, you can also include arbitrary text in your format, such as "Points: %i".

Modes

There are currently four modes of counting.

EFLabelCountingMethod.linear
Counts linearly from the start to the end.

EFLabelCountingMethod.easeIn
Ease In starts out slow and speeds up counting as it gets to the end, stopping suddenly at the final value.

EFLabelCountingMethod.easeOut
Ease Out starts out fast and slows down as it gets to the destination value.

EFLabelCountingMethod.easeInOut
Ease In/Out starts out slow, speeds up towards the middle, and then slows down as it approaches the destination. It is a nice, smooth curve that looks great, and is the default method.

PS

The first version of EFCountingLabel is converted from UICountingLabel.

Author

EyreFree, [email protected]

License

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

efcountinglabel's People

Contributors

eyrefree avatar hakefinance avatar rwinzhang avatar nikolaisa avatar

Stargazers

Apptek Studios avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.