iOS捕捉键盘移动(根据键盘移动view跟随上下)

在QQ上我们经常会看到进入聊天页面时,输入框会跟着键盘的弹起而上下移动,本demo就是仿照此功能而设计

代码如下:

#pragma mark- vc
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"与xxx聊天中";
    
        //从IOS7开始,导航条和状态栏合为一体,而且呈半透明状,viewController.view的坐标0,0点在屏幕左上方,如果子视图位置在view的顶部,就会被导航条遮盖,所以,viewController会对自身的第一个子视图(scrollView)顶部添加64像素的contentInset,避免scrollView内容被查盖。
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _table.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    _table.delegate = self;
    _table.dataSource = self;
    
        //设置分割线的样式。
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    
        //scrollView滑动时键盘消失;
        //_table.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    [self loadSubView];
    
        //监听键盘frame变化的通知,键盘位置发生变化时,让输入框位置也随之变化。
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveKeyboardFrameChangeNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    _array = [[NSMutableArray alloc] initWithCapacity:0];
    
    [_table registerNib:[UINib nibWithNibName:@"SelfCell" bundle:nil] forCellReuseIdentifier:@"self"];
    
    [_table registerNib:[UINib nibWithNibName:@"OtherCell" bundle:nil] forCellReuseIdentifier:@"other"];
}

- (void)loadSubView{
    _inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 627, 375, 40)];
    [self.view addSubview:_inputView];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:_inputView.bounds];
    imgView.image = [UIImage imageNamed:@"chatinputbg.png"];
    [_inputView addSubview:imgView];
    [imgView release];
    
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(3, 3, 369, 34)];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
        //设置return键的样式
    _textField.returnKeyType = UIReturnKeySend;
        //自动可用return键。
    _textField.enablesReturnKeyAutomatically = YES;
        //英文首字母是否大写。
    _textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        //清除键的显示方式。
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_inputView addSubview:_textField];
    
    _textField.delegate = self;
}

#pragma mark- notificationMethod
- (void)receiveKeyboardFrameChangeNotification:(NSNotification *)noti{
        //NSLog(@"%@",noti.userInfo);
    
        //NSValue值类,基本类型和结构体不能直接放入数组和字典中,如果需要把结构体放入数组或字典,那么必须把结构体转换为NSValue,然后把NSValue放入字典或数组。
    NSValue *value = [noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
        //CGRect rect = [value CGRectValue];
    CGRect rect;
    [value getValue:&rect];
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:[[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
        //7是键盘动画专用速率
    [UIView setAnimationCurve:[[noti.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]];
    _inputView.frame = CGRectMake(0, rect.origin.y-40, 375, 40);
    _table.frame = CGRectMake(0, 0, 375, _inputView.frame.origin.y);
    if (_array.count>1) {
        [_table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_array.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
    }
    [UIView commitAnimations];
}

#pragma mark- scrollViewDelegate
    //scrollView将要开始拖拽时的回调方法。
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

#pragma mark- textField
    //return键点击时调用。
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [_array addObject:textField.text];
    textField.text = @"";
    [_table reloadData];
    
        //scrollToRowAtIndexPath让tableView滚动到某一行,atScrollPosition参数是让这一行滚动出来之后显示在tableView上的位置(如果有可能)
    [_table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_array.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:YES];
    return YES;
}

#pragma mark- table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *text = [_array objectAtIndex:indexPath.row];
        //boundingRectWithSize计算出文本显示的边框大小。第一个参数是限定的宽度和高度。第二个参数是计算选项,第三个参数是计算的文本的具体属性。
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineBreakMode = NSLineBreakByCharWrapping;
    CGRect rect = [text boundingRectWithSize:CGSizeMake(220, 0) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17],NSParagraphStyleAttributeName:style} context:nil];
    [style release];
    
    if (indexPath.row%2 == 0) {
        SelfCell *cell = [tableView dequeueReusableCellWithIdentifier:@"self"];
        cell.messageLabel.frame = CGRectMake(30, 10, rect.size.width, rect.size.height);
        cell.bubbleImageView.frame = CGRectMake(20, 5, rect.size.width+20, rect.size.height+10);
        cell.messageLabel.text = text;
        return cell;
    }else{
        OtherCell *cell = [tableView dequeueReusableCellWithIdentifier:@"other"];
        cell.messageLabel.frame = CGRectMake(375-rect.size.width-30, 10, rect.size.width, rect.size.height);
        cell.bubbleImageView.frame = CGRectMake(375-rect.size.width-40, 5, rect.size.width+20, rect.size.height+10);
        cell.messageLabel.text = text;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *text = [_array objectAtIndex:indexPath.row];
    CGRect rect = [text boundingRectWithSize:CGSizeMake(220, 0) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    return rect.size.height+20;
}
cell中

- (void)awakeFromNib {
    // Initialization code
    UIImage *img = [UIImage imageNamed:@"bubble.png"];
        //stretchableImageWithLeftCapWidth设置边帽,能够使图片在拉伸或压缩时只按照某个像素点的颜色进行填充。(保证图片拉伸时不变形)
    self.bubbleImageView.image = [img stretchableImageWithLeftCapWidth:25 topCapHeight:20];
    
    self.messageLabel.numberOfLines = 0;
        //折行方式
    self.messageLabel.lineBreakMode = NSLineBreakByCharWrapping;
}
效果如下:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值