• 怎么样才能方便的隐藏键盘


    键盘的显示很方便,但是键盘比较方便的隐藏就不是那么容易了

    下图1

    呼出了键盘后,可以在键盘的右边添加一个按钮,如果用户想隐藏键盘,就点此按钮,就可以了。

    但是在IOS5的情况下,中文输入法在键盘右边的按钮就会被选词区遮盖了。

    请看图2

     

     隐藏键盘的按钮被系统的选词区遮盖了,基于这个状况,希望用户能够点击键盘区域以外的地方也能隐藏键盘。

     所以如何知道用户点击键盘以为区域就显得很重要了

     由于在ViewController中不能捕获以下Touch事件,以下Touch主要是针对UIView的

     - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
     - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
     - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
     - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

     所以我们无法通过Touches来判读用户是否点击了键盘以外区域,所以我选择了UITapGestureRecognizer,通过捕获用户的tap事件,来隐藏键盘。 

      在ViewDidLoad里添加如下的方法

     -(void)addTapGuesture
    {
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard:)];
        [tapGesture setNumberOfTapsRequired:1];
        [self.view addGestureRecognizer:tapGesture];
        [tapGesture release];

    } 

     我以为一切运行良好。。。。。

     可惜实际的情况并不是那么理想,比如画面1中有UISwitch和UIButton, UITapGestureRecognizer会隐藏了UISwitch 和UIButton等一切能响应用户Tap的控件。

     还好UITapGestureRecognizer有过滤功能,它能够让你选择哪些控件需要响应UITapGestureRecognizer哪些不需要
     实现UIGestureRecognizerDelegate委托的如下方法,YES是需要响应,NO为不需要响应

     - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

    {
        
        if ([touch.view isKindOfClass:[CustomUISwitch class]] || [touch.view isKindOfClass: [UIButton class]] ) 
        {    
            return NO;
        }
        return YES;
    }

    同时addTapCuesture也需要做如下修改,添加  tapGesture.delegate = self;

    修改后的代码如下

    -(void)addTapGuesture
    {
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(confirmAction:)];
        tapGesture.delegate = self;
        [tapGesture setNumberOfTapsRequired:1];
        [self.view addGestureRecognizer:tapGesture];
        [tapGesture release];

    }

    但是其它问题又来了,如果UIViewController还需要管理UITableView,那怎么办?用户在点击TableViewCell不隐藏键盘和滚动TableView的时候键盘就需要隐藏键盘。

    (当然你也可以设置为在点击UITableView的时候也隐藏键盘,但这个基本不要过滤) 

    首先解决第一个问题,点击TableViewCell的时候能不隐藏键盘

    还是回到上面的思路,在gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch里过滤掉Cell。 

    通过Debug可以知道点击UITableView的时候,UITouch传递的是Cell的contentView 。它是一个UIView,无法用isKindOfClass区别,但是UIView有tag我们在创建Cell的时候赋予一个tag,
    cell.contentView.tag = k_cell_view_tag;

    然后在gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 里判断,方法如下 :

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
        if (touch.view.tag ==k_cell_view_tag ) 
        {    
            return NO;
        }
        return YES;

    } 

    接着解决第二个问题,如何解决滑动TableView的时候隐藏键盘 

    我们在UIScrollDelegate的scrollViewWillBeginDragging隐藏键盘 

     - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

    {
        [self hideKeyBoard];
    }

     到此,用户想隐藏键盘就非常方便了。

    可以参考这个地址http://stackoverflow.com/questions/594181/uitableview-and-keyboard-scrolling-issue 

  • 相关阅读:
    Interesting Finds: 2008.09.15~2008.09.21
    Interesting Finds: 2008.10.05~2008.10.07
    Interesting Find: 2008.10.02
    Interesting Finds: 2008.10.13~2008.10.15
    Interesting Finds: 2008.09.29
    Interesting Finds: 2008.10.08~2008.10.09
    Interesting Finds: 2008.09.22~2008.09.27
    Interesting Finds: 2008.10.12
    Interesting Finds: 2008.10.16~2008.10.18
    9月27号
  • 原文地址:https://www.cnblogs.com/likwo/p/2260698.html
Copyright © 2020-2023  润新知