当前位置:网站首页>Using RAC to realize the sending logic of verification code
Using RAC to realize the sending logic of verification code
2022-07-28 05:06:00 【Lu_ Ca】
Preface
In the project, mobile phone verification code login and registration pages have the need to send verification code . I have been using my own timer to do the corresponding processing before , I've been watching RAC Something related , I just see that the timer conforms to some previous design scenarios , It's a simple implementation .
Import RAC
My training program is Objective-C Project , So before podfile Add... To the document pod ‘ReactiveObjC’, ‘~> 3.0.0’, direct pod install
then , Import the header file on the required page #import "ReactiveObjC.h".
Specific code
#import "ViewController.h"
#import "ReactiveObjC.h"
@interface ViewController ()
@property (nonatomic, strong) RACDisposable *btnDispose;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton new];
[button setTitle:@" Send verification code " forState:UIControlStateNormal];
button.backgroundColor = [UIColor blueColor];
button.layer.cornerRadius = 0;
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.frame = CGRectMake(100, 100, 200, 40);
[self.view addSubview:button];
// Create the semaphore of the button click , And listen for click events
[[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
button.enabled = NO;
__block int time = 5;
//subscribeNext Return to one RACDisposable, Can be used to unsubscribe
self.btnDispose = [[RACSignal interval:1.0 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {
// After the timer executes , utilize time To monitor whether the countdown is over
time--;
NSString *title = time == 0 ? @" Send verification code " : [NSString stringWithFormat:@" Please wait %d Second retransmission ",time];
[button setTitle:title forState:UIControlStateNormal];
button.enabled = time == 0 ? YES : NO;
button.layer.cornerRadius = 15;
button.backgroundColor = [UIColor greenColor];
if(time == 0){
button.layer.cornerRadius = 0;
button.backgroundColor = [UIColor blueColor];
// Unsubscribe
[self.btnDispose dispose];
}
}];
}];
}I changed the rounded corner and background color of the button after the countdown , It is convenient to distinguish different states . The renderings are as follows

边栏推荐
- list indices must be integers or slices, not tuple
- Look at the experience of n-year software testing summarized by people who came over the test
- 【CPU占用高】software_reporter_tool.exe
- HashSet add
- RT_ Use of thread mailbox
- C language classic 100 question exercise (1~21)
- Leetcode 18. sum of four numbers
- C语言ATM自动取款机系统项目的设计与开发
- FreeRTOS startup process, coding style and debugging method
- How to analyze fans' interests?
猜你喜欢

flink思维导图

Redis type

Visual studio 2019 new OpenGL project does not need to reconfigure the environment

【内功心法】——函数栈帧的创建和销毁(C实现)

Histogram of pyplot module of Matplotlib (hist(): basic parameter, return value)

Observable time series data downsampling practice in Prometheus

Driving the powerful functions of EVM and xcm, how subwallet enables Boca and moonbeam

RT_ Use of thread message queue

CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?

What is the core value of testing?
随机推荐
【ARXIV2203】CMX: Cross-Modal Fusion for RGB-X Semantic Segmentation with Transformers
Gan: generative advantageous nets -- paper analysis and the mathematical concepts behind it
HDU 3592 World Exhibition (differential constraint)
数据安全逐步落地,必须紧盯泄露源头
Testcafe provides automatic waiting mechanism and live operation mode
[learning record] data enhancement 1
Visual studio 2019 new OpenGL project does not need to reconfigure the environment
Pipe /createpipe
After a year of unemployment, I learned to do cross-border e-commerce and earned 520000. Only then did I know that going to work really delayed making money!
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
在外包公司两年了,感觉快要废了
System clock failure of database fault tolerance
[daily question 1] 735. Planetary collision
FreeRTOS个人笔记-任务通知
list indices must be integers or slices, not tuple
The default isolation level of MySQL is RR. Why does Alibaba and other large manufacturers change to RC?
C语言ATM自动取款机系统项目的设计与开发
微服务故障模式与构建弹性系统
【ARXIV2205】EdgeViTs: Competing Light-weight CNNs on Mobile Devices with Vision Transformers
The research group passed the thesis defense successfully