Code Monkey home page Code Monkey logo

mztimerlabel's Introduction

MZTimerLabel

ScreenShot

ScreenShot2

###Purpose

MZTimerLabel is a UILabel subclass, which is a handy way to use UILabel as a countdown timer or stopwatch just like that in Apple Clock App with just 2 lines of code. MZTimerLabel also provides delegate method for you to define the action when the timer finished.

Auther: MineS Chan

Remark: This is my first iOS plugin project on github, please accept my apologize if any bad coding.

###Requirements

  • ARC
  • iOS 5.0+

###Installations

####Manual

  1. Download or clone MZTimerLabel, add MZTimerLabel.h and MZTimerLabel.m souce files into your project.
  2. #import "MZTimerLabel.h" whereever you need it.

####CocoaPods

(Unformilar with CocoaPods yet? It's a dependency management tool for iOS and Mac, check it out!)

  1. Add pod 'MZTimerLabel', '~> 0.4.1' to your podfiles

###Easy Example

To use MZTimerLabel as a stopwatch and counter, you need only 2 lines.

   MZTimerLabel *stopwatch = [[MZTimerLabel alloc] initWithLabel:aUILabel];
   [stopwatch start];

Easy? If you are looking for a timer, things is just similar.

   MZTimerLabel *timer = [[MZTimerLabel alloc] initWithLabel:aUILabel andTimerType:MZTimerLabelTypeTimer];
   [timer setCountDownTime:60];
   [timer start];

Now the timer will start counting from 60 to 0 ;)

###Custom Appearance

As MZTimerLabel is a UILabel subclass, you can directly allocate it as a normal UILabel and customize timeLabel property just like usual.

   MZTimerLabel *redStopwatch = [[MZTimerLabel alloc] init];
   redStopwatch.frame = CGRectMake(100,50,100,20);
   redStopwatch.timeLabel.font = [UIFont systemFontOfSize:20.0f];
   redStopwatch.timeLabel.textColor = [UIColor redColor];
   [self.view addSubview:redStopwatch];
   [redStopwatch start];

MZTimerLabel uses 00:00:00 (HH:mm:ss) as time format, if you prefer using another format such as including milliseconds.Your can set your time format like below.

timerExample4.timeFormat = @"HH:mm:ss SS";

###Control the timer

You can start,pause,reset your timer with your custom control, set your control up and call these methods:

-(void)start;
-(void)pause;
-(void)reset;

And you control the time at the begining or during runtime with these methods

-(void)setCountDownTime:(NSTimeInterval)time;
-(void)setStopWatchTime:(NSTimeInterval)time;
-(void)setCountDownToDate:(NSDate*)date;
-(void)addTimeCountedByTime:(NSTimeInterval)timeToAdd;

###Timer Finish Handling

Usually when you need a timer, you need to deal with it after it finished counting. Following are 2 examples showing how to do it using delegate and block methods.

####Delegate

First, set the delegate of the timer label.

timer.delegate = self;

And then implement MZTimerLabelDelegate protocol in your dedicated class

@interface ViewController : UIViewController<MZTimerLabelDelegate>

Finally, implement the delegate method timerLabel:finshedCountDownTimerWithTimeWithTime:

-(void)timerLabel:(MZTimerLabel*)timerLabel finshedCountDownTimerWithTime:(NSTimeInterval)countTime{
   //time is up, what should I do master?
}

####Blocks

Block is a very convenient way to handle the callbacks, MZTimerLabel makes your life even easier.

   MZTimerLabel *timer = [[MZTimerLabel alloc] initWithLabel:aUILabel andTimerType:MZTimerLabelTypeTimer];
   [timer3 setCountDownTime:60]; 
   [timer startWithEndingBlock:^(NSTimeInterval countTime) {
       //oh my god it's awesome!!
   }];

###More Examples

Please check the demo project I provided, with well explained example code inside.

###License This code is distributed under the terms and conditions of the MIT license.

What's coming up next?

  1. Submit to CocaPods
  2. Better performance.
  3. Your suggestions!:D

Donations

Donate

mztimerlabel's People

Contributors

michalz-railwaymen avatar mineschan avatar robnadin avatar

Watchers

 avatar  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.