登录界面下一级界面验证手机号码页面的Demo
#import "NextView.h"
#define VIEW_WIDTH self.frame.size.width
@interface NextView ()
@property (nonatomic , strong) UILabel *toastLabel;//提示验证码发送到哪个手机号的label
@property (nonatomic , strong) UILabel *backLabel;//背景
@property (nonatomic , strong) UITextField *codeTextFeild;//验证码
@property (nonatomic , strong) UIButton *timeButton;//倒计时
@property (nonatomic , strong) UILabel *verticalLineLabel;//竖线
@property (nonatomic , strong) UIButton *registerbutton;//注册按钮
@end
@implementation NextView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self addSubview:self.toastLabel];
[self addSubview:self.backLabel];
[self addSubview:self.codeTextFeild];
[self addSubview:self.timeButton];
[self addSubview:self.verticalLineLabel];
[self addSubview:self.registerbutton];
[self GCDTimer];
}
return self;
}
#pragma mark - 布局子控件
- (void)layoutSubviews {
[super layoutSubviews];
[self addAutoLayout];
}
#pragma mark - Masonry自动布局
- (void)addAutoLayout{
__weak typeof(self) weakSelf = self;
[_toastLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(35);
make.top.right.equalTo(weakSelf);
make.left.equalTo(weakSelf.mas_left).offset(15);
}];
[_backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(44);
make.top.equalTo(weakSelf.toastLabel.mas_bottom);
make.left.equalTo(weakSelf.mas_left).offset(-1);
make.right.equalTo(weakSelf.mas_right).offset(1);
}];
[_codeTextFeild mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(weakSelf.backLabel.mas_left).offset(15);
make.top.bottom.equalTo(weakSelf.backLabel);
make.right.equalTo(weakSelf.backLabel.mas_right).offset(VIEW_WIDTH - 110);
}];
[_timeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(100, 44));
make.right.top.equalTo(weakSelf.backLabel);
}];
[_verticalLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(1, 30));
make.centerY.equalTo(weakSelf.backLabel.mas_centerY);
make.right.equalTo(weakSelf.timeButton.mas_left).offset(-1);
}];
[_registerbutton mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(35);
make.top.equalTo(weakSelf.codeTextFeild.mas_bottom).offset(15);
make.left.equalTo(weakSelf.mas_left).offset(16);
make.right.equalTo(weakSelf.mas_right).offset(-16);
}];
}
#pragma mark - 重写公开属性(phoneNumberString)setter方法
- (void)setPhoneNumberString:(NSString *)phoneNumberString {
_phoneNumberString = phoneNumberString;
//提示手机号标题富文本属性设置:
_toastLabel.attributedText = [self makeToastPhoneNumberLabelAttributed];
//倒计时按钮富文本属性设置:
[_timeButton setAttributedTitle:[self makeTimeButtonAttributed:1] forState:UIControlStateNormal];
}
#pragma mark - 提示手机号标题富文本属性方法
- (NSMutableAttributedString *)makeToastPhoneNumberLabelAttributed {
//设置颜色富文本属性:
//验证码已发送到(灰色) +86(浅蓝色):
NSMutableAttributedString *numberString = [[NSMutableAttributedString alloc] initWithString:@"验证码已发送到" attributes:@{NSForegroundColorAttributeName:RGB(139, 139, 139)}];
NSMutableAttributedString *phoneString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" +86 %@" , _phoneNumberString] attributes:@{NSForegroundColorAttributeName:RGB(56, 166, 243)}];
[numberString insertAttributedString:phoneString atIndex:numberString.length];
return numberString;
}
#pragma mark - 倒计时按钮富文本属性方法
- (NSMutableAttributedString *)makeTimeButtonAttributed:(NSInteger)time {
//时间富文本属性修改:
NSMutableAttributedString *timeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%li" , time] attributes:@{NSForegroundColorAttributeName:RGB(56, 166, 243)}];
NSMutableAttributedString *addString = [[NSMutableAttributedString alloc] initWithString:@"秒后重试" attributes:@{NSForegroundColorAttributeName:RGB(139, 139, 139)}];
[timeString insertAttributedString:addString atIndex:timeString.length];
return timeString;
}
#pragma mark - time
- (void)GCDTimer {
__block NSInteger time = 10;
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
//leewayInSeconds:落后时间;
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
if (time < 1) {
dispatch_source_cancel(timer);
//计时结束重新显示重新发送:
NSMutableAttributedString *returnString = [[NSMutableAttributedString alloc] initWithString:@"重新发送" attributes:@{NSForegroundColorAttributeName:RGB(56, 166, 243)}];
//主线程刷新UI:
dispatch_async(dispatch_get_main_queue(), ^{
//倒计时结束,按钮允许点击:
_timeButton.userInteractionEnabled = YES;
//时间小于1的时候文字变为"重新发送":
[_timeButton setAttributedTitle:returnString forState:UIControlStateNormal];
});
} else {
//主线程刷新UI:
dispatch_async(dispatch_get_main_queue(), ^{
//倒计时进行中,按钮不可点击:
_timeButton.userInteractionEnabled = NO;
[_timeButton setAttributedTitle:[self makeTimeButtonAttributed:time] forState:UIControlStateNormal];
});
time--;
}
});
dispatch_resume(timer);
}
#pragma mark - lazyLoad
- (UILabel *)toastLabel {
if (!_toastLabel) {
_toastLabel = [[UILabel alloc] init];
_toastLabel.text = @"验证码已发送到 +86";
_toastLabel.font = [UIFont systemFontOfSize:12.0f];
}
return _toastLabel;
}
- (UILabel *)backLabel {
if (!_backLabel) {
_backLabel = [[UILabel alloc] init];
_backLabel.backgroundColor = [UIColor whiteColor];
_backLabel.layer.borderWidth = 1;
_backLabel.layer.borderColor = RGB(188, 188, 188).CGColor;
}
return _backLabel;
}
- (UITextField *)codeTextFeild {
if (!_codeTextFeild) {
_codeTextFeild = [[UITextField alloc] init];
_codeTextFeild.delegate = self;
_codeTextFeild.placeholder = @"请输入验证码...";
[_codeTextFeild addTarget:self action:@selector(codeTextChangeText:) forControlEvents:UIControlEventEditingChanged];
}
return _codeTextFeild;
}
- (UIButton *)timeButton {
if (!_timeButton) {
_timeButton = [UIButton buttonWithType:UIButtonTypeCustom];
// [_timeButton setTitle:@"30秒后重试" forState:UIControlStateNormal];
//添加点击方法点击这个 _timeButton 就调用倒计时的方法:
[_timeButton addTarget:self action:@selector(GCDTimer) forControlEvents:UIControlEventTouchUpInside];
[_timeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
return _timeButton;
}
- (UILabel *)verticalLineLabel {
if (!_verticalLineLabel) {
_verticalLineLabel = [[UILabel alloc] init];
_verticalLineLabel.backgroundColor = RGB(188, 188, 188);
}
return _verticalLineLabel;
}
- (UIButton *)registerbutton {
if (!_registerbutton) {
_registerbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[_registerbutton setTitle:@"注 册" forState:UIControlStateNormal];
_registerbutton.backgroundColor = RGB(229, 229, 229);
[_registerbutton setTitleColor:RGB(132, 132, 132) forState:UIControlStateNormal];
[_registerbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
_registerbutton.selected = NO;
[_registerbutton addTarget:self action:@selector(registerbuttonAction) forControlEvents:UIControlEventTouchUpInside];
}
return _registerbutton;
}
#pragma makr - 验证码方法
- (void)codeTextChangeText:(UITextField *)textField {
if (textField.text.length == 6) {
self.registerbutton.userInteractionEnabled = YES;
self.registerbutton.backgroundColor = RGB(56, 166, 241);
self.registerbutton.selected = YES;
} else {
self.registerbutton.userInteractionEnabled = NO;
self.registerbutton.backgroundColor = RGB(229, 229, 229);
self.registerbutton.selected = NO;
}
}
#pragma mark -
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (range.location == 6) {
return NO;
}
return YES;
}
#pragma mark - 注册方法
- (void)registerbuttonAction {
}
@end