驗(yàn)證手機(jī)號(hào)碼頁(yè)面

登錄界面下一級(jí)界面驗(yàn)證手機(jī)號(hào)碼頁(yè)面的Demo

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

@interface NextView () <UITextFieldDelegate>
@property (nonatomic , strong) UILabel *toastLabel;//提示驗(yàn)證碼發(fā)送到哪個(gè)手機(jī)號(hào)的label
@property (nonatomic , strong) UILabel *backLabel;//背景
@property (nonatomic , strong) UITextField *codeTextFeild;//驗(yàn)證碼
@property (nonatomic , strong) UIButton *timeButton;//倒計(jì)時(shí)
@property (nonatomic , strong) UILabel *verticalLineLabel;//豎線
@property (nonatomic , strong) UIButton *registerbutton;//注冊(cè)按鈕
@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自動(dòng)布局
- (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 - 重寫(xiě)公開(kāi)屬性(phoneNumberString)setter方法
- (void)setPhoneNumberString:(NSString *)phoneNumberString {
    _phoneNumberString = phoneNumberString;
    
    //提示手機(jī)號(hào)標(biāo)題富文本屬性設(shè)置:
    _toastLabel.attributedText = [self makeToastPhoneNumberLabelAttributed];
    //倒計(jì)時(shí)按鈕富文本屬性設(shè)置:
    [_timeButton setAttributedTitle:[self makeTimeButtonAttributed:1] forState:UIControlStateNormal];
}


#pragma mark - 提示手機(jī)號(hào)標(biāo)題富文本屬性方法
- (NSMutableAttributedString *)makeToastPhoneNumberLabelAttributed {
    //設(shè)置顏色富文本屬性:
    //驗(yàn)證碼已發(fā)送到(灰色) +86(淺藍(lán)色):
    NSMutableAttributedString *numberString = [[NSMutableAttributedString alloc] initWithString:@"驗(yàn)證碼已發(fā)送到" 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 - 倒計(jì)時(shí)按鈕富文本屬性方法
- (NSMutableAttributedString *)makeTimeButtonAttributed:(NSInteger)time {
    //時(shí)間富文本屬性修改:
    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:落后時(shí)間;
    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);
            //計(jì)時(shí)結(jié)束重新顯示重新發(fā)送:
            NSMutableAttributedString *returnString = [[NSMutableAttributedString alloc] initWithString:@"重新發(fā)送" attributes:@{NSForegroundColorAttributeName:RGB(56, 166, 243)}];
            //主線程刷新UI:
            dispatch_async(dispatch_get_main_queue(), ^{
                //倒計(jì)時(shí)結(jié)束,按鈕允許點(diǎn)擊:
                _timeButton.userInteractionEnabled = YES;
                //時(shí)間小于1的時(shí)候文字變?yōu)?重新發(fā)送":
                [_timeButton setAttributedTitle:returnString forState:UIControlStateNormal];
            });
        } else {
            //主線程刷新UI:
            dispatch_async(dispatch_get_main_queue(), ^{
                //倒計(jì)時(shí)進(jìn)行中,按鈕不可點(diǎn)擊:
                _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 = @"驗(yàn)證碼已發(fā)送到 +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 = @"請(qǐng)輸入驗(yàn)證碼...";
        [_codeTextFeild addTarget:self action:@selector(codeTextChangeText:) forControlEvents:UIControlEventEditingChanged];
        
    }
    return _codeTextFeild;
}

- (UIButton *)timeButton {
    if (!_timeButton) {
        _timeButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_timeButton setTitle:@"30秒后重試" forState:UIControlStateNormal];
        //添加點(diǎn)擊方法點(diǎn)擊這個(gè) _timeButton 就調(diào)用倒計(jì)時(shí)的方法:
        [_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:@"注 冊(cè)" 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 - 驗(yàn)證碼方法
- (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 - 注冊(cè)方法
- (void)registerbuttonAction {
    
}

@end

愿編程讓這個(gè)世界更美好

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,036評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,309評(píng)論 4 61
  • 有時(shí)候人們?yōu)榱朔棚w自由的夢(mèng)想,為了追求堅(jiān)定的目標(biāo),不得不大聲說(shuō):“不?!? 在自己還沒(méi)有喜歡那個(gè)事物...
    百合花劉浩然閱讀 222評(píng)論 0 0
  • 廣州橋下 彈琴的人 隱在某個(gè)角落 聽(tīng)琴的人 不見(jiàn)蹤影 我看著行人 也看著流浪者 開(kāi)始尋找 來(lái)訪的本意 燈紅酒綠 粗...
    子心yjr一廣州閱讀 195評(píng)論 3 1
  • 在美國(guó)的一家珠寶店,發(fā)生過(guò)這樣一件事情:一位店員在接待客戶的時(shí)候,因?yàn)槭置δ_亂把客戶的珍珠滾落到了地上。當(dāng)時(shí)店里人...
    大冰舟閱讀 222評(píng)論 0 0

友情鏈接更多精彩內(nèi)容