• 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
  • 相关阅读:
    jquery判断设备是否是手机
    jQuery -- touch事件之滑动判断(左右上下方向)
    sass制作雪碧图
    js时间字符串转为标准时间
    装箱和拆箱
    Dictionary泛型集合实现山寨版金山词霸
    泛型集合
    ArrayList集合与索引器及Hashtable
    黑马程序员--静态方法和静态类
    黑马程序员--多态练习(手机工厂)
  • 原文地址:https://www.cnblogs.com/songxing10000/p/5025614.html
Copyright © 2020-2023  润新知