// FaceView.m
//
// Created by PXD on 15-5-3.
// Copyright (c) 2015ๅนด PXD. All rights reserved.
//
#import "FaceView.h"
@interface FaceView ()
@property (nonatomic, strong) UIScrollView *faceScrollView;
@end
@implementation FaceView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
// self.faceScrollView = [[UIScrollView alloc] initWithFrame:frame];
// _faceScrollView.backgroundColor = [UIColor lightGrayColor];
// [self addSubview:_faceScrollView];
}
return self;
}
@end
//
// ChatViewController.m
// QQ
//
// Created by PXD on 15-4-29.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import "ChatViewController.h"
#import "FriendsModel.h"
#import "FaceView.h"
#import "AppDelegate.h"
typedef enum {
kFaceButtonStatusFace = 1,
kFaceButtonStatusKeyboard
}kFaceButtonStatus;
@interface ChatViewController ()
@property (nonatomic, strong) FriendsModel *model;
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) UITextField *inputTextField;
@property (nonatomic, strong) UITextField *showingTextField;
@property (nonatomic, strong) FaceView *faceView;
@property (nonatomic, strong) UIView *showingOperationView;
@property (nonatomic, strong) UIView *reallyInputOperationView;
@end
@implementation ChatViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
friendModel:(FriendsModel *)model{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.model = model;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self uiInitial];
//注册(监听)一个键盘弹出来的消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardDidShowNotification object:nil];
//注册(监听)一个键盘隐藏的消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardDidHideNotification object:nil];
}
- (void)keyboardShow:(NSNotification *)notifi{
//让inputTextField 作为第一响应者
[_inputTextField becomeFirstResponder];
//tableView上移
self.myTableView.frame = CGRectMake(0, 20 + 44, 320, 568 - 253 - 44 - 64);
//显示最后一行
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:29 inSection:0];
[self.myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
- (void)keyboardHidden:(NSNotification *)notifi{
//将输入框的内容 显示到 showingTextField上
_showingTextField.text = _inputTextField.text;
//tableView还原
self.myTableView.frame = CGRectMake(0, 64, 320, 568-64-44);
}
- (void)uiInitial{
UIImage *bgImage = [UIImage imageNamed:@"bg"];
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
bgImageView.image = bgImage;
[self.view addSubview:bgImageView];
self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 320, 568-64-44) style:UITableViewStylePlain];
_myTableView.delegate = self;
_myTableView.dataSource =self;
_myTableView.backgroundColor = [UIColor clearColor];
_myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:_myTableView];
//真正的输入视图
self.reallyInputOperationView = [self createMessageInputOperationView];
_reallyInputOperationView.frame = CGRectMake(0, 0, 320, 44);
self.inputTextField = [[UITextField alloc]initWithFrame:CGRectMake(30, 10, 200, 24)];
_inputTextField.placeholder = @"message";
_inputTextField.borderStyle = UITextBorderStyleLine;
_inputTextField.delegate = self;
[_reallyInputOperationView addSubview:_inputTextField];
//显示的输入视图
self.showingOperationView = [self createMessageInputOperationView];
[self.view addSubview:_showingOperationView];
self.showingTextField = [[UITextField alloc]initWithFrame:CGRectMake(30, 10, 200, 24)];
_showingTextField.placeholder = @"message";
_showingTextField.borderStyle = UITextBorderStyleLine;
_showingTextField.inputAccessoryView = _reallyInputOperationView;
[_showingOperationView addSubview:_showingTextField];
}
- (void)setModel:(FriendsModel *)model{
if (_model != model) {
_model = model;
}
//设置导航栏的标题 为联系人的名字
self.title = _model.friendName;
}
#pragma mark -- TableDelegate&DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.text = [NSString stringWithFormat:@"row_%ld", indexPath.row];
return cell;
}
- (UIView *)createMessageInputOperationView{
//背景视图
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 568-44, 320, 44)];
[bgView setBackgroundColor:[UIColor lightTextColor]];
//笑脸
UIButton *faceButton = [UIButton buttonWithType:UIButtonTypeCustom];
[faceButton setFrame:CGRectMake(240, 7, 30, 30)];
[faceButton setBackgroundImage:[UIImage imageNamed:@"face"] forState:UIControlStateNormal];
[faceButton addTarget:self action:@selector(faceButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
faceButton.tag = kFaceButtonStatusKeyboard;
[bgView addSubview:faceButton];
//add
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
[addButton setFrame:CGRectMake(280, 7, 30, 30)];
[bgView addSubview:addButton];
return bgView;
}
//重写faceView的get方法
- (FaceView *)faceView{
if (_faceView == nil) {
_faceView = [[FaceView alloc] initWithFrame:CGRectMake(0, 568-253, 320, 253)];
_faceView.hidden = YES;
_faceView.backgroundColor = [UIColor redColor];
[self.view addSubview:_faceView];
}
return _faceView;
}
#pragma mark -- FaceButtonAction
- (void)faceButtonDidClicked:(UIButton *)sender{
if (sender.tag == kFaceButtonStatusKeyboard) {
//显示表情视图
self.faceView.hidden = NO;
//改变button的状态
sender.tag = kFaceButtonStatusFace;
self.showingOperationView.frame = CGRectMake(0, 568-253-44, 320, 44);
UIButton *showViewButton = (UIButton *)[self.showingOperationView viewWithTag:kFaceButtonStatusKeyboard];
showViewButton.tag = kFaceButtonStatusFace;
[self.inputTextField resignFirstResponder];
[self.showingTextField resignFirstResponder];
} else {
//隐藏表情视图
self.faceView.hidden = YES;
sender.tag = kFaceButtonStatusKeyboard;
[_showingTextField becomeFirstResponder];
self.showingOperationView.frame = CGRectMake(0, 568-44, 320, 44);
UIButton *reallyViewButton = (UIButton *)[self.reallyInputOperationView viewWithTag:kFaceButtonStatusFace];
reallyViewButton.tag = kFaceButtonStatusKeyboard;
}
}
#pragma mark -- UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//取消textfield的第一响应者
[_inputTextField resignFirstResponder];
[_showingTextField resignFirstResponder];
return YES;
}
#pragma mark -- UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//取消textfield的第一响应者
[_inputTextField resignFirstResponder];
[_showingTextField resignFirstResponder];
}
@end