驗證手機號碼頁面

登錄界面下一級界面驗證手機號碼頁面的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閱讀 229,763評論 6 539
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,238評論 3 428
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,823評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,604評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,339評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,713評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,712評論 3 445
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,893評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,448評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,201評論 3 357
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,397評論 1 372
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,944評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,631評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,033評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,321評論 1 293
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,128評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,347評論 2 377

推薦閱讀更多精彩內容

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