用RAC实现登陆界面的倒计时

来源:互联网 发布:校园宿舍管理系统java 编辑:程序博客网 时间:2024/05/21 10:27
当需要手机注册或者验证的需求 有个1分钟的倒计时  具体代码如下
- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    self.remainSeconds = 60;    self.startCheckTimer = 1;    self.label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 20)] ;    self.label.backgroundColor = [UIColor redColor];    [self.view addSubview:self.label];    [self setTime];}- (void)setTime{    @weakify(self);    RACSignal* signal = [[[RACSignal interval:1                                  onScheduler:[RACScheduler mainThreadScheduler]] startWith:[NSDate date]] map:^id(NSDate* value) {        @strongify(self);        if(self.remainSeconds > 0 && self.startCheckTimer){            self.remainSeconds = self.remainSeconds - 1;            if(self.remainSeconds == 0){                self.startCheckTimer = 0;            }        }        NSString *text = [NSString stringWithFormat:@"未收到验证码?请稍等%d秒",self.remainSeconds];        return text; }];    [signal subscribeNext:^(NSString* x) {        @strongify(self);        self.label.text = x;    }];    RAC(self.reSendButton, enabled) = [RACSignal                                       combineLatest:@[                                                       RACObserve(self, remainSeconds),                                                       RACObserve(self, startCheckTimer)                                                       ]                                       reduce:^id(NSNumber* remain, NSNumber* startCheck) {                                           return@( remain.intValue == 0 && _startCheckTimer == 0);                                       }];}- (void)setResendButton:button{    _reSendButton = button;    @weakify(self);    [[self.reSendButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {        @strongify(self);        self.reSendButton.enabled = FALSE;        self.remainSeconds = 60;        self.startCheckTimer = 1;//        [self startResendVerifyCode];    }];}

以上简单的实现了倒计时60s
0 0
原创粉丝点击