十一岁的加重songxing10000…… ------ 回到博主首页

iOS注册,找回密码时用到的获取验证码

#import "ViewController.h"
#import "NSTimer+BlocksKit.h"

@interface ViewController ()
{
    NSTimer *vCodeButtonTimer1, *vCodeButtonTimer2;

}
@property (weak, nonatomic) IBOutlet UIButton *button;
@end

@implementation ViewController
- (IBAction)action:(id)sender {

    __block int time = 8;
    vCodeButtonTimer1 = [NSTimer bk_scheduledTimerWithTimeInterval:1 block:^(NSTimer *timer) {
        [self.button setTitle:[NSString stringWithFormat:@"%ds后重新获取", time --] forState:UIControlStateDisabled];
        [self.button setEnabled:NO];
    } repeats:YES];
    
    [vCodeButtonTimer1 fire];
    
    vCodeButtonTimer2 = [NSTimer bk_scheduledTimerWithTimeInterval:7 block:^(NSTimer *timer) {
        [vCodeButtonTimer1 invalidate];
        vCodeButtonTimer1 = nil;
        [self.button setEnabled:YES];
    } repeats:NO];
}

- (void)dealloc


{


    [vCodeButtonTimer2 invalidate];


    [vCodeButtonTimer1 invalidate];


}

@end

 

用到的blockit文件

#import <Foundation/Foundation.h>

/** Simple category on NSTimer to give it blocks capability.

 Created by [Jiva DeVoe](https://github.com/jivadevoe) as `NSTimer-Blocks`.
*/
@interface NSTimer (BlocksKit)

/** Creates and returns a block-based NSTimer object and schedules it on the current run loop.

 @param inTimeInterval The number of seconds between firings of the timer.
 @param inBlock The block that the NSTimer fires.
 @param inRepeats If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.
 @return A new NSTimer object, configured according to the specified parameters.
 */
+ (NSTimer *)bk_scheduledTimerWithTimeInterval:(NSTimeInterval)inTimeInterval block:(void (^)(NSTimer *timer))inBlock repeats:(BOOL)inRepeats;

/** Creates and returns a block-based NSTimer initialized with the specified block.

 You must add the new timer to a run loop, using `-addTimer:forMode:`. Then,
 after seconds seconds have elapsed, the timer fires the block.  If the timer
 is configured to repeat, there is no need to subsequently re-add the timer.

 @param inTimeInterval The number of seconds between firings of the timer.
 @param inBlock The block that the NSTimer fires.
 @param inRepeats If YES, the timer will repeatedly reschedule itself until invalidated. If NO, the timer will be invalidated after it fires.
 @return A new NSTimer object, configured according to the specified parameters.
 */
+ (NSTimer *)bk_timerWithTimeInterval:(NSTimeInterval)inTimeInterval block:(void (^)(NSTimer *timer))inBlock repeats:(BOOL)inRepeats;

@end
#import "NSTimer+BlocksKit.h"

@interface NSTimer (BlocksKitPrivate)

+ (void)bk_executeBlockFromTimer:(NSTimer *)aTimer;

@end

@implementation NSTimer (BlocksKit)

+ (id)bk_scheduledTimerWithTimeInterval:(NSTimeInterval)inTimeInterval block:(void (^)(NSTimer *timer))block repeats:(BOOL)inRepeats
{
    NSParameterAssert(block != nil);
    return [self scheduledTimerWithTimeInterval:inTimeInterval target:self selector:@selector(bk_executeBlockFromTimer:) userInfo:[block copy] repeats:inRepeats];
}

+ (id)bk_timerWithTimeInterval:(NSTimeInterval)inTimeInterval block:(void (^)(NSTimer *timer))block repeats:(BOOL)inRepeats
{
    NSParameterAssert(block != nil);
    return [self timerWithTimeInterval:inTimeInterval target:self selector:@selector(bk_executeBlockFromTimer:) userInfo:[block copy] repeats:inRepeats];
}

+ (void)bk_executeBlockFromTimer:(NSTimer *)aTimer {
    void (^block)(NSTimer *) = [aTimer userInfo];
    if (block) block(aTimer);
}

@end

 

posted @ 2015-12-07 11:31  songxing10000  阅读(532)  评论(0编辑  收藏  举报