监听键盘,tableview动态变动到键盘上方

@interface XXXViewController ()


@property (nonatomic,strong)UITapGestureRecognizer *keyboardTap;

@property (nonatomic, assign) NSInteger keyboardTagRow;//键盘弹出,界面上滑

@property (nonatomic, assign) NSInteger keyboardTagSection;//键盘弹出,界面上滑

@property (nonatomic, assign) CGFloat scrollFloat;//键盘弹出,界面上滑


@end


@implementation XXXViewController

-(void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    [self addKeyboardNotification];//键盘监听

}

-(void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    //释放

    [[NSNotificationCenter defaultCenterremoveObserver:self name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenterremoveObserver:self name:UIKeyboardWillHideNotification object:nil];

    

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}

#pragma mark -键盘监听

- (void)addKeyboardNotification

{

    //增加监听,当键盘出现或改变时收出消息

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillShow:)

                                                 name:UIKeyboardWillShowNotification

                                               object:nil];

    

    //增加监听,当键退出时收出消息

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillHide:)

                                                 name:UIKeyboardWillHideNotification

                                               object:nil];

    

    _keyboardTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidekeyBoard:)];

    _keyboardTap.cancelsTouchesInView = NO;

}

//当键盘出现或改变时调用

- (void)keyboardWillShow:(NSNotification *)aNotification

{

    //获取键盘的高度

    NSDictionary *userInfo = [aNotification userInfo];

    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    int keyboardHeight = keyboardRect.size.height;

    

    NSTimeInterval animationDuration=0.30f;

    

    

    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:_keyboardTagRow inSection:_keyboardTagSection];

    CGRect rectInTableView =[_myTableView rectForRowAtIndexPath:indexPath];

    CGRect rectInSuperview = [_myTableView convertRect:rectInTableView toView:self.view];

    if (keyboardHeight>(kScreenHeight-(rectInSuperview.origin.y+rectInSuperview.size.height))) {

        [UIView animateWithDuration:animationDuration animations:^{

            _myTableView.contentInset = UIEdgeInsetsMake(64, 0, keyboardHeight, 0);

            [_myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

        }];

    }

    [self.view addGestureRecognizer:_keyboardTap];

}


//当键退出时调用

- (void)keyboardWillHide:(NSNotification *)aNotification{

    _myTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

    [self.view removeGestureRecognizer:_keyboardTap];

}

#pragma mark - 点击隐藏键盘

-(void)hidekeyBoard:(UITapGestureRecognizer *)gesture{

    UIView *view = gesture.view;

    [self hideKeyboardImpl:view];

}


-(void) hideKeyboardImpl:(UIView*) view

{

    [self findResponderAndResign:view];

}


-(BOOL) findResponderAndResign:(UIView*) view

{

    if (view)

    {

        if ([view isFirstResponder])

        {

            [view resignFirstResponder];

            return YES;

        }

        NSArray *children = [view subviews];

        for (int i = 0; i < children.count; ++i)

        {

            UIView *child = children[i];

            if ([self findResponderAndResign:child])

            {

                return YES;

            }

        }

    }

    return NO;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值