SQTimerLabel 0.0.3

SQTimerLabel 0.0.3

semnyqu 维护。



  • 作者:
  • semnyqu

MZTimerLabel

License Platforms Cocoapod Latest Version Carthage compatible Downloads with CocoaPods

ScreenShot

ScreenShot2

用途

MZTimerLabel 是一个 UILabel 子类,它是使用 UILabel 作为倒计时器或秒表的一个便捷方式,就像 Apple 时钟应用中那样,只需两行代码即可。MZTimerLabel 还提供代理方法以供定义定时器完成时的操作。

作者:MineS Chan 和优秀的 贡献者

备注:这是我第一个在 GitHub 上的 iOS 插件项目,如果代码有任何问题,请接受我的道歉。

要求

  • ARC
  • iOS 5.0+

安装

手动

  1. 下载或克隆MZTimerLabel,将MZTimerLabel.hMZTimerLabel.m源文件添加到您的项目中。
  2. 在需要的地方#import "MZTimerLabel.h"

CocoaPods

(还不熟悉CocoaPods?它是iOS和Mac的依赖管理工具,去看看吧!)

pod 'MZTimerLabel'

Carthage

另一个依赖管理工具是Carthage,它没有集中存储库。

github "mineschan/MZTimerLabel"

简单示例

要将MZTimerLabel用作计时器和计数器,只需要2行代码。

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

简单吗?如果您正在寻找计时器,操作非常类似。

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

现在计时器将从60开始倒数,直到0。

自定义外观

由于MZTimerLabel是UILabel的子类,您可以将其作为普通的UILabel进行分配,并像通常一样自定义timeLabel属性。

   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使用00:00:00 (HH:mm:ss)作为时间格式,如果您想使用其他格式(例如包含毫秒),可以按照如下设置您的格式:

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

控制计时器

您可以使用自定义控件开始、暂停和重置计时器,设置好控件后调用这些方法。

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

获取器和设置器

您可以使用这些属性和方法来控制开始时或在运行时的时间值和行为。

@property (assign) BOOL shouldCountBeyondHHLimit;   //see example #12
@property (assign) BOOL resetTimerAfterFinish;      //see example #7

-(void)setCountDownTime:(NSTimeInterval)time;
-(void)setStopWatchTime:(NSTimeInterval)time;
-(void)setCountDownToDate:(NSDate*)date;
-(void)addTimeCountedByTime:(NSTimeInterval)timeToAdd; //see example #10, #11

若想获取计时器的信息,请看以下方法。

@property (assign,readonly) BOOL counting;  //see example #4-7

- (NSTimeInterval)getTimeCounted;    //see example #3
- (NSTimeInterval)getTimeRemaining;  //see example #3
- (NSTimeInterval)getCountDownTime;  

计时器结束处理

通常,在需要计时器时,你可能需要在它完成后进行处理。以下有两个示例,展示了如何使用 delegateblock 方法来实现这一功能。

代理

首先,设置计时器标签的代理。

timer.delegate = self;

然后在你的专用类中实现 MZTimerLabelDelegate 协议

@interface ViewController : UIViewController<MZTimerLabelDelegate>

最后,实现代理方法 timerLabel:finshedCountDownTimerWithTimeWithTime:

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

Blocks

Block 是处理回调的一种非常方便的方法,MZTimerLabel 使您的编程生活更加轻松。

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

或者单独设置

   [timer3 setCountDownTime:60]; 
   timer.endedBlock = ^(NSTimeInterval countTime) {
       //oh my gosh, it's awesome!!
   };
   [timer start];

更多示例

请查看我提供的演示项目,其中包含了详细说明的示例代码。

许可证

此代码在MIT许可证的条款和条件下分发。

接下来是什么?

  1. 提交到CocoaPods
  2. 更好的性能。
  3. 你的建议!:D