Files
peko-ios/YuMi/Modules/YMLogin/View/NewLogin/XPLoginInputView.m
2023-08-14 14:39:41 +08:00

164 lines
5.4 KiB
Objective-C

//
// YMLoginInputView.m
// YUMI
//
// Created by XY on 2023/2/14.
//
#import "XPLoginInputView.h"
#import "DJDKMIMOMColor.h"
#import <Masonry.h>
#import <ReactiveObjC.h>
@interface XPLoginInputView()
@property (nonatomic, strong) dispatch_source_t timer;
@property (nonatomic,strong) UIImageView *areaImageView;
@end
@implementation XPLoginInputView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = UIColorFromRGB(0xF5F6FA);
self.layer.cornerRadius = kGetScaleWidth(52)/2;
self.layer.masksToBounds = YES;
[self createUI];
}
return self;
}
- (void)createUI {
UIStackView *stackView = [[UIStackView alloc] init];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.distribution = UIStackViewDistributionFill;
stackView.alignment = UIStackViewAlignmentCenter;
stackView.spacing = kGetScaleWidth(8);
[self addSubview:stackView];
UIImageView * areaImageView = [[UIImageView alloc] init];
areaImageView.userInteractionEnabled = YES;
areaImageView.image = [UIImage imageNamed:@"login_area_arrow"];
areaImageView.userInteractionEnabled = NO;
/// 区号
UIButton *areaCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[areaCodeBtn setTitle:@"+852" forState:UIControlStateNormal];
[areaCodeBtn setTitleColor:UIColorFromRGB(0x1F1B4F) forState:UIControlStateNormal];
areaCodeBtn.titleLabel.font = kFontMedium(16);
_areaCodeBtn = areaCodeBtn;
areaCodeBtn.userInteractionEnabled = NO;
UIStackView *areaStackView = [[UIStackView alloc] init];
areaStackView.axis = UILayoutConstraintAxisHorizontal;
areaStackView.distribution = UIStackViewDistributionFill;
areaStackView.alignment = UIStackViewAlignmentCenter;
areaStackView.spacing = kGetScaleWidth(8);
UIButton *areaBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[areaBtn addTarget:self action:@selector(areaChooseClicked) forControlEvents:UIControlEventTouchUpInside];
[areaStackView addSubview:areaBtn];
[areaStackView addArrangedSubview:areaCodeBtn];
[areaStackView addArrangedSubview:areaImageView];
[stackView addArrangedSubview:areaStackView];
self.areaStackView = areaStackView;
/// 输入框
UITextField *inputTextField = [[UITextField alloc] init];
inputTextField.textColor = UIColorFromRGB(0x1F1B4F);
inputTextField.font = kFontRegular(14);
[stackView addArrangedSubview:inputTextField];
self.inputTextField = inputTextField;
/// 获取验证码
UIButton *smsCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[smsCodeBtn setTitle:YMLocalizedString(@"XPLoginInputView0") forState:UIControlStateNormal];
[smsCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
smsCodeBtn.titleLabel.font = kFontMedium(14);
smsCodeBtn.layer.cornerRadius = kGetScaleWidth(38)/2;
smsCodeBtn.layer.masksToBounds = YES;
smsCodeBtn.backgroundColor = UIColorFromRGB(0x9168FA);
[smsCodeBtn addTarget:self action:@selector(smsCodeBtnClicked) forControlEvents:UIControlEventTouchUpInside];
[stackView addArrangedSubview:smsCodeBtn];
self.smsCodeBtn = smsCodeBtn;
[stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(kGetScaleWidth(24));
make.right.mas_equalTo(-kGetScaleWidth(24));
make.top.bottom.mas_equalTo(0);
}];
[areaImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(kGetScaleWidth(12));
make.height.mas_equalTo(kGetScaleWidth(8));
}];
[areaCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_lessThanOrEqualTo(kGetScaleWidth(60));
make.height.mas_equalTo(stackView);
}];
[inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(stackView);
}];
[smsCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(kGetScaleWidth(102));
make.height.mas_equalTo(kGetScaleWidth(38));
}];
[areaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(areaStackView);
}];
self.areaStackView.hidden = YES;
self.smsCodeBtn.hidden = YES;
}
- (void)smsCodeBtnClicked {
if (self.delegate && [self.delegate respondsToSelector:@selector(smsCodeAction)]) {
[self.delegate smsCodeAction];
}
}
- (void)areaChooseClicked {
if (self.delegate && [self.delegate respondsToSelector:@selector(areaListAction)]) {
[self.delegate areaListAction];
}
}
//开启倒计时
- (void)fireTimer {
__block NSInteger count = 60;
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
@weakify(self);
dispatch_source_set_event_handler(timer, ^{
@strongify(self);
count--;
if (count < 0) {
[self.smsCodeBtn setTitle:YMLocalizedString(@"XPLoginInputView1") forState:UIControlStateNormal];
self.smsCodeBtn.userInteractionEnabled = YES;
dispatch_cancel(self.timer);
}else{
[self.smsCodeBtn setTitle:[NSString stringWithFormat:@"%lds",count] forState:UIControlStateNormal];
self.smsCodeBtn.userInteractionEnabled = NO;
}
});
dispatch_resume(timer);
self.timer = timer;
}
@end