貓貓學IOS(二十四)UI之註冊案例,iosui

來源:互聯網
上載者:User

貓貓學IOS(二十四)UI之註冊案例,iosui

貓貓分享,必須精品

素材代碼地址:http://blog.csdn.net/u013357243/article/details/45165591
原創文章,歡迎轉載。轉載請註明:翟乃玉的部落格
地址:http://blog.csdn.net/u013357243?viewmode=contents

先看效果

製作思路

貓貓在做這個的時候,首先用stroyboard畫出來介面UI,這個很簡單,不多說了,然後下一步就是自訂xib做鍵盤上面的那一欄了,需要自己做xib還有view,詳細代碼下面會有,當我們點擊的時候,我們的鍵盤的frame變化形成彈出的效果,這個在前面QQ案例裡面有,其實就是通知的用法,其中我們自訂工具條的“上一個”“下一個”“完成”這些按鈕,我們用了代理的方式來完成的。

代碼,注意點代理

首先我們要自己寫自己的代理:

@protocol NYKeyboardToolDelegate <NSObject> -(void)keyboardTool:(NYKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType;@end

然後要在我們設定代理的類那讓他實現我們的協議

@interface NYViewController () <NYKeyboardToolDelegate>

建立工具列 ,設定代理,把所有的UITextField遍曆到放到全域變數_fields中來,設定屬性

//建立工具列    NYKeyboardTool *keyboardTool = [NYKeyboardTool keyBoardTool];    //設定代理    keyboardTool.delegate = self;    //1,擷取輸入框容器的所有子控制項    NSArray *views = [self.inputContainer subviews];    //建立一個數組儲存textField    NSMutableArray *fieldM = [NSMutableArray array];    //2,遍曆    for (UIView *childView in views) {        //如果子控制項是UITextField的時候,設定inputAccessoryView        if ([childView isKindOfClass:[UITextField class]]) {            UITextField *tf = (UITextField *)childView;            tf.inputAccessoryView = keyboardTool;            //把textField添加到數組中。            [fieldM addObject:tf];            _fields = fieldM;        }    }

記住要實現協議方法否則工具條按鈕沒用

#pragma mark - NYKeyboardToolDelegate代理 鍵盤工具條的代理-(void)keyboardTool:(NYKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType{    //擷取當前的響應者的索引    int currentIndex = [self getCurrentResponderIndex];    NSLog(@"當前的響應者 %d",currentIndex);    if (itemType == KeyboardItemTypePrevious) {        NSLog(@"上一個");        //讓上一個field成功響應者        [self showProviousField:currentIndex];    }else if(itemType == KeyboardItemTypeNext){        NSLog(@"下一個");        //讓下一個field成功響應者        [self showNextField:currentIndex];    }else{        NSLog(@"完成");        [self touchesBegan:nil withEvent:nil];    }}//讓上一個field成功響應者-(void)showProviousField:(int)currentIndex{    int proviousIndex =  currentIndex - 1;    if (proviousIndex >= 0  ) {        UITextField *proviousTf = [_fields objectAtIndex:proviousIndex];        [proviousTf becomeFirstResponder];    }}//讓下一個field成功響應者-(void)showNextField:(int)currentIndex{    int nextIndex =  currentIndex + 1;    //下一個索引不能超過_fields數組的個數    if (nextIndex < _fields.count) {        //讓當前響應者分發去        UITextField *currentTf = [_fields objectAtIndex:currentIndex];        [currentTf resignFirstResponder];        UITextField *nextTf = [_fields objectAtIndex:nextIndex];        [nextTf becomeFirstResponder];    }}
鍵盤的frame變化動畫,還有通知

初始化自訂鍵盤

//1:初始化自訂鍵盤-(void)setupCustomKeyboard{    UIDatePicker *datePicker = [[UIDatePicker alloc] init];    //設定地區    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];    //設定模式    datePicker.datePickerMode = UIDatePickerModeDate;    //把datePicker放到birthday的鍵盤中    self.birthdayField.inputView = datePicker;}

鍵盤frame變化除了,開通通知
建立監聽(通知)

//建立監聽    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(kbFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];

鍵盤frame的變化

//鍵盤的frame的變換-(void)kbFrameChange:(NSNotification *)noti{    //改變window的背景顏色    self.view.window.backgroundColor = self.inputAccessoryView.backgroundColor;    //  鍵盤退出的frame    CGRect frame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];    //鍵盤即時y    CGFloat kbEndY = frame.origin.y;    //擷取當前的響應者     int currentIndex = [self getCurrentResponderIndex];    UITextField *currentTF = _fields[currentIndex];    //應該改變的高度:    CGFloat tfMaxY = CGRectGetMaxY(currentTF.frame) + self.inputContainer.frame.origin.y;    //如果textfield的最大值在於鍵盤的y坐,才要往上移    if (tfMaxY > kbEndY) {        [UIView animateWithDuration:0.25 animations:^{            self.view.transform = CGAffineTransformMakeTranslation(0, kbEndY - tfMaxY);        }];    }else{        [UIView animateWithDuration:0.25 animations:^{            self.view.transform = CGAffineTransformIdentity;        }];    } }
其他小地方

關閉鍵盤

//開始觸摸的方法-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    //關閉鍵盤    [self.view endEditing:YES];}

view中又很多UITextField 然後我們想知道哪一個開啟了鍵盤,就是擷取哪一個是響應者

//擷取當前  textField的響應者在_fields中的索引//返回-1代表沒有找到-(int)getCurrentResponderIndex{    //遍曆所有的textField擷取響應值    for (UITextField *tf in _fields) {        if (tf.isFirstResponder) {            return [_fields indexOfObject:tf];        }    }    return -1;}

UIDatePicker日期鍵盤的用法:

    UIDatePicker *datePicker = [[UIDatePicker alloc] init];    //設定地區    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];    //設定模式    datePicker.datePickerMode = UIDatePickerModeDate;    //把datePicker放到birthday的鍵盤中    self.birthdayField.inputView = datePicker;

ps:建立iOS交流學習群:304570962 可以加貓貓QQ:1764541256 或則znycat 讓我們一起努力學習吧。
翟乃玉的部落格
地址:http://blog.csdn.net/u013357243?viewmode=contents

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.