zoukankan      html  css  js  c++  java
  • iOS 系统数字键盘左下角加确定按钮

    首先在 viewWillAppear 方法中注册监听相应的键盘通知,并且要在 viewWillDisappear 方法中注销通知
    - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //注册键盘显示通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil]; //注册键盘隐藏通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; //注销键盘显示通知 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; }

    处理键盘弹出和收起事件

    // 键盘出现处理事件
    - (void)handleKeyboardDidShow:(NSNotification *)notification
    {
        // NSNotification中的 userInfo字典中包含键盘的位置和大小等信息
        NSDictionary *userInfo = [notification userInfo];
        // UIKeyboardAnimationDurationUserInfoKey 对应键盘弹出的动画时间
        CGFloat animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
        // UIKeyboardAnimationCurveUserInfoKey 对应键盘弹出的动画类型
        NSInteger animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
        //数字彩,数字键盘添加“完成”按钮
        if (doneInKeyboardButton){
            
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:animationDuration];//设置添加按钮的动画时间
            [UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve];//设置添加按钮的动画类型
            
            //设置自定制按钮的添加位置(这里为数字键盘添加“完成”按钮)
            doneInKeyboardButton.transform=CGAffineTransformMakeTranslation(0, -53);
            
            [UIView commitAnimations];
        }
        
    }
    
    // 键盘消失处理事件
    - (void)handleKeyboardWillHide:(NSNotification *)notification
    {
        // NSNotification中的 userInfo字典中包含键盘的位置和大小等信息
        NSDictionary *userInfo = [notification userInfo];
        // UIKeyboardAnimationDurationUserInfoKey 对应键盘收起的动画时间
        CGFloat animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
        
        if (doneInKeyboardButton.superview)
        {
            [UIView animateWithDuration:animationDuration animations:^{
                //动画内容,将自定制按钮移回初始位置
                doneInKeyboardButton.transform=CGAffineTransformIdentity;
            } completion:^(BOOL finished) {
                //动画结束后移除自定制的按钮
                [doneInKeyboardButton removeFromSuperview];
            }];
            
        }
    }

    在textField代理中调用加载按钮方法

    #pragma mark -- UITextFieldDelegate
    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        //初始化数字键盘的“完成”按钮
        if(doneInKeyboardButton.superview==nil)
            [self configDoneInKeyBoardButton];
    }
    - (void) addDoneButton{
        //获得键盘所在的window视图
        NSArray *array= [[UIApplication sharedApplication]windows];
        
        for (UIWindow *window in array) {
            
            NSString *str=NSStringFromClass([window class]);
            
            if ([str isEqualToString:@"UIRemoteKeyboardWindow"]) {
                
                [window addSubview:doneInKeyboardButton];
                
            }
            
        }
        
    }
    
    //点击“完成”按钮事件,收起键盘
    -(void)finishAction{
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];//关闭键盘
    }

    如果用户安装了其他输入法,会出错,因此在APPDelegate中禁止第三方的输入法

    -(BOOL)application:(UIApplication *)application shouldAllowExtensionPointIdentifier:(NSString *)extensionPointIdentifier{
        
        return NO;
    }

    用最好的情绪做最好的自我。开心是一天,不开心也是一天,不如天天开心。活在当下,生活就为你歌唱。
  • 相关阅读:
    Java基础01-JVM内存分析
    性能测试工具LoadRunner32-LR之windows性能监控Perfmon
    性能测试工具LoadRunner31-LR之链接mysql
    性能测试工具LoadRunner30-LR之监控Tomcat
    性能测试工具LoadRunner29-LR之测试java代码
    JS活动倒计时案例
    鼠标图片跟随案例
    如何实现网页PC端自动跳转到手机移动端(备用)
    JavaScript—12高级事件
    JavaScript—11 DOM基础的核心要点整理
  • 原文地址:https://www.cnblogs.com/lukunlun/p/5919823.html
Copyright © 2011-2022 走看看