AJ学IOS(24)UI之注册案例

AJ分享,必须精品

先看效果

这里写图片描述

制作思路

在做这个的时候,首先用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;
原文地址:https://www.cnblogs.com/luolianxi/p/4990352.html