驗證手機號碼頁面

登錄界面下一級界面驗證手機號碼頁面的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

愿編程讓這個世界更美好

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,466評論 25 708
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,251評論 4 61
  • 有時候人們為了放飛自由的夢想,為了追求堅定的目標,不得不大聲說:“不。” 在自己還沒有喜歡那個事物...
    百合花劉浩然閱讀 169評論 0 0
  • 廣州橋下 彈琴的人 隱在某個角落 聽琴的人 不見蹤影 我看著行人 也看著流浪者 開始尋找 來訪的本意 燈紅酒綠 粗...
    子心yjr一廣州閱讀 184評論 3 1
  • 在美國的一家珠寶店,發生過這樣一件事情:一位店員在接待客戶的時候,因為手忙腳亂把客戶的珍珠滾落到了地上。當時店里人...
    大冰舟閱讀 177評論 0 0