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

当需要手机注册或者验证的需求 有个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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
`direct` 修饰的变量可以使用 RAC,但需要注意一些细节。 由于 `direct` 是一个实例变量,而不是 `@property`,因此不能使用 `RAC` 提供的默认的 `@synthesize` 指令来自动生成访问器方法。如果想在 `RAC` 中使用 `direct` 变量,需要手动实现访问器方法。 下面是一个示例代码: ```objective-c @interface MyClass : NSObject @property (nonatomic, copy) NSString *indirectProperty; @end @implementation MyClass { NSString *_directProperty; } - (NSString *)directProperty { return _directProperty; } - (void)setDirectProperty:(NSString *)directProperty { _directProperty = [directProperty copy]; } @end ``` 这样,在使用 `RAC` 时,就可以通过访问器方法来访问 `direct` 变量了,例如: ```objective-c MyClass *obj = [[MyClass alloc] init]; [obj rac_observeKeyPath:@"directProperty" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld observer:nil block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) { NSLog(@"directProperty changed: %@", value); }]; obj.directProperty = @"new value"; ``` 需要注意的是,当使用 `RAC` 观察 `direct` 变量时,不能直接使用 `RACObserve` 宏,而需要使用 `rac_observeKeyPath:options:observer:block:` 方法来观察变量。这是因为 `RACObserve` 宏只能用来观察 `@property` 修饰的变量,不能用来观察实例变量。另外,需要手动实现访问器方法,才能让 `RAC` 正常工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值