验证手机号码页面

登录界面下一级界面验证手机号码页面的Demo

#import "NextView.h"
#define VIEW_WIDTH self.frame.size.width

@interface NextView () <UITextFieldDelegate>
@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 - <UITextFieldDelegate>
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (range.location == 6) {
        return NO;
    }
    return YES;
}


#pragma mark - 注册方法
- (void)registerbuttonAction {
    
}

@end

愿编程让这个世界更美好

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 198,932评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,554评论 2 375
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 145,894评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,442评论 1 268
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,347评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,899评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,325评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,980评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,196评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,163评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,085评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,826评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,389评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,501评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,753评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,171评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,616评论 2 339

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,226评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,973评论 4 60
  • 有时候人们为了放飞自由的梦想,为了追求坚定的目标,不得不大声说:“不。” 在自己还没有喜欢那个事物...
    百合花刘浩然阅读 140评论 0 0
  • 广州桥下 弹琴的人 隐在某个角落 听琴的人 不见踪影 我看着行人 也看着流浪者 开始寻找 来访的本意 灯红酒绿 粗...
    子心yjr一广州阅读 181评论 3 1
  • 在美国的一家珠宝店,发生过这样一件事情:一位店员在接待客户的时候,因为手忙脚乱把客户的珍珠滚落到了地上。当时店里人...
    大冰舟阅读 162评论 0 0