• 020606-04-聊天布局-键盘处理


    //
    //  XMGChatingViewController.h
    //  07-聊天布局
    #import <UIKit/UIKit.h>
    
    @interface XMGChatingViewController : UIViewController
    
    @end
    //
    //  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(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    }
    
    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    #pragma mark - 键盘处理
    - (void)keyboardWillChangeFrame:(NSNotification *)note {
        // 取出键盘最终的frame
        CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        // 取出键盘弹出需要花费的时间
        double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        
        // 修改约束
        self.bottomSpacing.constant = [UIScreen mainScreen].bounds.size.height - rect.origin.y;
        [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
    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    恕我直言,你可能误解了微服务
    准备情人节礼物比写代码难?来看看IT直男给女友们的礼物
    2019年微服务5大趋势,你pick哪个?
    拼多多通用优惠券漏洞被薅羊毛数千万 你的系统有反作弊防护吗?
    知物由学 | AI在Facebook清理有害内容上扮演了什么角色?
    打造业界最牛微服务,网易云这两位“大神”获了大奖
    网易云轻舟微服务斩获“创新产品奖”等两项大奖
    GIS中栅格数据结构的显示与计算
    未能找到tempselect2.cur的一部分
    地图下载3之超图瓦片下载工具
  • 原文地址:https://www.cnblogs.com/laugh/p/6508895.html
Copyright © 2020-2023  润新知