ios-点击屏幕,隐藏键盘

ios-点击屏幕,隐藏键盘

- (void)getFirstRegist{
    //结束键盘编辑
    __weak typeof(self)weakSelf = self;

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenKeyBoard)];//注意是UITapGestureRecognizer
    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];//主队列

    //在键盘出现之前,这个函数会被触发(键盘内置的方法,来获取这个通知中的消息)
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) {
        [weakSelf.view addGestureRecognizer:tapGestureRecognizer];
    }];
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) {
        [weakSelf.view removeGestureRecognizer:tapGestureRecognizer];
    }];
}

- (void)hiddenKeyBoard{
    [self.view endEditing:YES];
}
posted @ 2015-05-15 13:52  AbeDay  阅读(181)  评论(0编辑  收藏  举报