首页 > 代码库 > 020606-03-聊天布局-键盘处理

020606-03-聊天布局-键盘处理

 

 

////  XMGChatingViewController.m//  07-聊天布局#import "XMGChatingViewController.h"#import "XMGMessage.h"#import "XMGMessageCell.h"@interface XMGChatingViewController () <UITableViewDataSource, UITableViewDelegate>@property (nonatomic, strong) NSArray *messages;@property (weak, nonatomic) IBOutlet UITextField *messageField;@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomSpacing;@end@implementation XMGChatingViewController- (NSArray *)messages{    if (_messages == nil) {        // 加载plist中的字典数组        NSString *path = [[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil];        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];                // 字典数组 -> 模型数组        NSMutableArray *messageArray = [NSMutableArray array];        // 用来记录上一条消息模型        XMGMessage *lastMessage = nil;        for (NSDictionary *dict in dictArray) {            XMGMessage *message = [XMGMessage messageWithDict:dict];            message.hideTime = [message.time isEqualToString:lastMessage.time];            [messageArray addObject:message];                        lastMessage = message;        }                _messages = messageArray;    }    return _messages;}- (void)viewDidLoad {    [super viewDidLoad];        // 设置文本框左边的内容    UIView *leftView = [[UIView alloc] init];    leftView.frame = CGRectMake(0, 0, 10, 0);    self.messageField.leftView = leftView;    self.messageField.leftViewMode = UITextFieldViewModeAlways;        // 监听键盘通知    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];}- (void)dealloc{    [[NSNotificationCenter defaultCenter] removeObserver:self];}#pragma mark - 键盘处理- (void)keyboardWillShow:(NSNotification *)note {    // 取出键盘最终的frame    CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];    // 取出键盘弹出需要花费的时间    double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];    // 修改约束    self.bottomSpacing.constant = rect.size.height;    [UIView animateWithDuration:duration animations:^{        [self.view layoutIfNeeded];    }];}- (void)keyboardWillHide:(NSNotification *)note {    // 取出键盘弹出需要花费的时间    double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];    // 修改约束    self.bottomSpacing.constant = 0;    [UIView animateWithDuration:duration animations:^{        [self.view layoutIfNeeded];    }];}#pragma mark - <UITableViewDataSource>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.messages.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    XMGMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"message"];        cell.message = self.messages[indexPath.row];        return cell;}#pragma mark - <UITableViewDelegate>- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 200;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    XMGMessage *message = self.messages[indexPath.row];    return message.cellHeight;}- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{    // 退出键盘//    [self.messageField resignFirstResponder];//    [self.messageField endEditing:YES];    [self.view endEditing:YES];}@end

 

////  XMGTestViewController.h//  07-聊天布局#import <UIKit/UIKit.h>@interface XMGTestViewController : UIViewController@end
////  XMGTestViewController.m//  07-聊天布局#import "XMGTestViewController.h"@implementation XMGTestViewController- (void)viewDidLoad{    [super viewDidLoad];        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"aaa" object:nil];}- (void)receiveNotification:(NSNotification *)note{    }@end

 

////  XMGMessageCell.h//  07-聊天布局#import <UIKit/UIKit.h>@class XMGMessage;@interface XMGMessageCell : UITableViewCell@property (nonatomic, strong) XMGMessage *message;@end
////  XMGMessageCell.m//  07-聊天布局#import "XMGMessageCell.h"#import "XMGMessage.h"//define this constant if you want to use Masonry without the ‘mas_‘ prefix#define MAS_SHORTHAND//define this constant if you want to enable auto-boxing for default syntax#define MAS_SHORTHAND_GLOBALS#import "Masonry.h"@interface XMGMessageCell()@property (weak, nonatomic) IBOutlet UILabel *timeLabel;@property (weak, nonatomic) IBOutlet UIButton *textButton;@property (weak, nonatomic) IBOutlet UIImageView *iconView;@property (weak, nonatomic) IBOutlet UIButton *otherTextButton;@property (weak, nonatomic) IBOutlet UIImageView *otherIconView;@end@implementation XMGMessageCell- (void)awakeFromNib{//    self.textButton.contentEdgeInsets//    self.textButton.imageEdgeInsets//    self.textButton.titleEdgeInsets    self.textButton.titleLabel.numberOfLines = 0;    self.otherTextButton.titleLabel.numberOfLines = 0;//    self.textButton.titleLabel.backgroundColor = [UIColor redColor];//    self.otherTextButton.titleLabel.backgroundColor = [UIColor redColor];}- (void)setMessage:(XMGMessage *)message{    _message = message;        if (message.hideTime) { // 隐藏时间        self.timeLabel.hidden = YES;        [self.timeLabel updateConstraints:^(MASConstraintMaker *make) {            make.height.equalTo(0);        }];    } else { // 显示时间        self.timeLabel.text = message.time;        self.timeLabel.hidden = NO;        [self.timeLabel updateConstraints:^(MASConstraintMaker *make) {            make.height.equalTo(21);        }];    }        if (message.type == XMGMessageTypeMe) { // 右边        [self settingShowTextButton:self.textButton showIconView:self.iconView hideTextButton:self.otherTextButton hideIconView:self.otherIconView];    } else { // 左边        [self settingShowTextButton:self.otherTextButton showIconView:self.otherIconView hideTextButton:self.textButton hideIconView:self.iconView];    }}/** * 处理左右按钮、头像 */- (void)settingShowTextButton:(UIButton *)showTextButton showIconView:(UIImageView *)showIconView hideTextButton:(UIButton *)hideTextButton hideIconView:(UIImageView *)hideIconView{    hideTextButton.hidden = YES;    hideIconView.hidden = YES;        showTextButton.hidden = NO;    showIconView.hidden = NO;        // 设置按钮的文字    [showTextButton setTitle:self.message.text forState:UIControlStateNormal];        // 强制更新    [showTextButton layoutIfNeeded];        // 设置按钮的高度就是titleLabel的高度    [showTextButton updateConstraints:^(MASConstraintMaker *make) {        CGFloat buttonH = showTextButton.titleLabel.frame.size.height + 30;        make.height.equalTo(buttonH);    }];        // 强制更新    [showTextButton layoutIfNeeded];        // 计算当前cell的高度    CGFloat buttonMaxY = CGRectGetMaxY(showTextButton.frame);    CGFloat iconMaxY = CGRectGetMaxY(showIconView.frame);    self.message.cellHeight = MAX(buttonMaxY, iconMaxY) + 10;}@end

 

020606-03-聊天布局-键盘处理