(素材源碼)貓貓學IOS(二十四)UI之註冊案例

來源:互聯網
上載者:User

標籤:效果   ui   ios   代碼   註冊   

貓貓分享,必須精品

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

先看效果

代碼NYViewController.m
////  NYViewController.m//  註冊////  Created by apple on 15-4-19.//  Copyright (c) 2015年 znycat. All rights reserved.//#import "NYViewController.h"#import "NYKeyboardTool.h"@interface NYViewController () <NYKeyboardToolDelegate>{    NSArray *_fields;//儲存所有的TextField}//輸入框容器view@property (weak, nonatomic) IBOutlet UIView *inputContainer;@property (weak, nonatomic) IBOutlet UITextField *birthdayField;@end@implementation NYViewController- (void)viewDidLoad{    [super viewDidLoad];    //1:初始化自訂鍵盤    [self setupCustomKeyboard];    //2:設定每一個textfield的鍵盤工具列(inputAccessoryView)    [self setupKeyboardTool];    //3:監聽鍵盤的事件    [self setupKeyboardNotification];}//1:初始化自訂鍵盤-(void)setupCustomKeyboard{    UIDatePicker *datePicker = [[UIDatePicker alloc] init];    //設定地區    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];    //設定模式    datePicker.datePickerMode = UIDatePickerModeDate;    //把datePicker放到birthday的鍵盤中    self.birthdayField.inputView = datePicker;}//2:設定每一個textfield的鍵盤工具列(inputAccessoryView)-(void)setupKeyboardTool{    //建立工具列    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;        }    }}//3:監聽鍵盤的事件-(void)setupKeyboardNotification{    //建立監聽    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(kbFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];}//鍵盤的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;        }];    }}//擷取當前  textField的響應者在_fields中的索引//返回-1代表沒有找到-(int)getCurrentResponderIndex{    //遍曆所有的textField擷取響應值    for (UITextField *tf in _fields) {        if (tf.isFirstResponder) {            return [_fields indexOfObject:tf];        }    }    return -1;}//開始觸摸的方法-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    //關閉鍵盤    [self.view endEditing:YES];}#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];    }}@end
自訂工具條代碼

NYKeyboardTool.h

////  NYKeyboardTool.h//  註冊////  Created by apple on 15-4-19.//  Copyright (c) 2015年 znycat. All rights reserved.//#import <UIKit/UIKit.h>@class NYKeyboardTool;typedef enum {    KeyboardItemTypePrevious,//上一個    KeyboardItemTypeNext,//下一個    KeyboardItemTypeDone//完成}KeyboardItemType;@protocol NYKeyboardToolDelegate <NSObject> -(void)keyboardTool:(NYKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType;@end@interface NYKeyboardTool : UITableViewHeaderFooterView//添加代理@property (nonatomic, weak) id<NYKeyboardToolDelegate> delegate;//建立對象+(instancetype)keyBoardTool;@end

NYKeyboardTool.m

////  NYKeyboardTool.m//  註冊////  Created by apple on 15-4-19.//  Copyright (c) 2015年 znycat. All rights reserved.//#import "NYKeyboardTool.h"@interface NYKeyboardTool()/**上一個*/- (IBAction)previous:(id)sender;/**下一個*/- (IBAction)next:(id)sender;/**完成*/- (IBAction)done:(id)sender;@end@implementation NYKeyboardTool//建立對象+(instancetype)keyBoardTool{    return [[[NSBundle mainBundle]loadNibNamed:@"NYKeyboardTool" owner:nil options:nil]lastObject];}- (IBAction)previous:(id)sender {    //判斷代理有沒有實現相應的方法。    if ([self.delegate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {        [self.delegate keyboardTool:self didClickItemType:KeyboardItemTypePrevious];    }}- (IBAction)next:(id)sender {    //判斷代理有沒有實現相應的方法。    if ([self.delegate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {        [self.delegate keyboardTool:self didClickItemType:KeyboardItemTypeNext];    }}- (IBAction)done:(id)sender {    //判斷代理有沒有實現相應的方法。    if ([self.delegate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {        [self.delegate keyboardTool:self didClickItemType:KeyboardItemTypeDone];    }}@end

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

(素材源碼)貓貓學IOS(二十四)UI之註冊案例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.