zoukankan      html  css  js  c++  java
  • 020606-06-聊天布局-键盘处理

    //  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;
    @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];
        // 修改transform
        [UIView animateWithDuration:duration animations:^{
            CGFloat ty = [UIScreen mainScreen].bounds.size.height - rect.origin.y;
            self.view.transform = CGAffineTransformMakeTranslation(0, - ty);
        }];
    }
    
    #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
    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    dos下 批处理 用 pause 可以在最后暂停 查看结果信息 build.bat
    flash jquery 调用摄像头 vue chrome49浏览器
    pandownload 百度网盘 下载
    webpack安装包的时候 1程序目录不要在C盘 2路径不要有中文 3用cnpm
    import * as tools from '@/libs/tools' 导入组件的时候 如果里面都是单独导入的,可以用 *加as起个别名使用
    markdown test
    谷歌浏览器 加安全地址 快捷方式加参数 chrome
    vue validate多表单验证思考 之前写过一个里外层,现在觉得不合适,应该平行的写,然后都给ret,最后判断ret 再做出反应,这样整体表单的所有验证就都报验证,然后最后提交的时候把组件内的对象合并到总的对象,再提交
    js引入的数组 会被页面缓存,如需要被强制性不缓存,请用function return 就ok了
    FiraCode 字体 => 箭头函数变成 整体 还有 等于 不等于
  • 原文地址:https://www.cnblogs.com/laugh/p/6508924.html
Copyright © 2011-2022 走看看