zoukankan      html  css  js  c++  java
  • iOS 切换键盘

    1.点击toolbar的加号按钮切换键盘

           切换前                 切换后

          

    2 要实现上述效果

    2.1 需要监听键盘的弹出隐藏

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

     1 /**
     2  *  键盘即将隐藏
     3  */
     4 - (void)keyboardWillHide:(NSNotification *)note
     5 {
     6     // 1.键盘弹出需要的时间
     7     CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
     8     // 2.动画
     9     [UIView animateWithDuration:duration animations:^{
    10         self.toolbar.transform = CGAffineTransformIdentity;
    11     }];
    12 }
     1 /**
     2  *  键盘即将弹出
     3  */
     4 - (void)keyboardWillShow:(NSNotification *)note
     5 {
     6     // 1.键盘弹出需要的时间
     7     CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
     8     // 2.动画
     9     [UIView animateWithDuration:duration animations:^{
    10         // 取出键盘高度
    11         CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    12         CGFloat keyboardH = keyboardF.size.height;
    13         self.toolbar.transform = CGAffineTransformMakeTranslation(0, - keyboardH);
    14     }];
    15 }

    2.2 toolbar的懒加载方法

     1 - (UIToolbar *)toolbar{
     2     if (_toolbar == nil) {
     3         _toolbar = [[UIToolbar alloc]init];
     4         _toolbar.frame = CGRectMake(0, screenHeight - kToolbarHeight , screenWidth, kToolbarHeight);
     5         [self.view addSubview:_toolbar];
     6         
     7         // 切换键盘的按钮
     8         UIButton *changeKeybord = [UIButton buttonWithType:UIButtonTypeContactAdd];
     9         [_toolbar addSubview:changeKeybord];
    10 
    11         [changeKeybord addTarget:self action:@selector(changeKeybord) forControlEvents:UIControlEventTouchDown];
    12     }
    13     return _toolbar;
    14 }

    2.3 实现changeKeybord方法

     1 - (void)changeKeybord
     2 {
     3     if (self.mytext.inputView) { //inputView存在则说明是自定义键盘
     4         self.mytext.inputView = nil; //把自定义键盘清空,则就变回系统键盘
     5     }else{ //反之,inputView不存在,则说明当前显示的是系统键盘
     6         UIView *newKeybord = [[UIView alloc]init];  // 实例化一个新键盘
     7         newKeybord.bounds = CGRectMake(0, 0, 320, 216);
     8         newKeybord.backgroundColor = [UIColor yellowColor];
     9         self.mytext.inputView = newKeybord; //将新键盘赋值给inputView
    10     }
    11 
    12     // 下面这句不能省,因为切换键盘后,只有先隐藏再弹出,才会调出新键盘
    13     [self.mytext resignFirstResponder];
    14     // 为了让键盘切换交互效果更顺畅,增加延迟,如果不增加延迟,会出现:旧键盘还没完全隐藏,新键盘就已经弹出了
    15     dispatch_after((uint64_t)3.0, dispatch_get_main_queue(), ^{
    16         [self.mytext becomeFirstResponder];
    17     });
    18     
    19 }

    2.4 自定义一个新键盘

    未完待续

  • 相关阅读:
    (zt)再给正直一次机会(最新进展)
    迁勇
    一本书、四部电影
    巴乔到北京了
    MLDN
    (zt)沉默是美德(转自连岳)
    十分钟
    不推荐两部电影
    Project Processing ...... Requirement
    Oracle Data Guard Linux 平台 Logical Standby 创建实例
  • 原文地址:https://www.cnblogs.com/oumygade/p/4223025.html
Copyright © 2011-2022 走看看