#import "IMKChattingViewController.h"
#import "IMCelebrityCell.h"
#import "IMChatCell.h"
#import "IMBottomView.h"
#import "UIImageView+WebCache.h"
#import "IMBuyView.h"
#define naviHeight 44
#define kBottomViewHeightDefault 43
#define keyboardShow 1
#define keyboardHide 2
#define kTextViewDefaultHeight 70
#define textViewBgToTop 6
#define textViewCursorToTop 1
#define textViewHeightLimit 6
#define textViewBgAndCursor 5
typedefenum
{
allHide,
allShow,
boardShow,
boardHide,
faceViewShow,
}kFrameState;
@interfaceIMKChattingViewController ()
@end
@implementation IMKChattingViewController
{
IMBottomView *bottomView;
IMBuyView *buyView;
BOOL faceShowing;
int keyboardHeight;
BOOL hideAll;
kFrameState frameState;
NSNotification *keyboardNoti;
CGRect bottomViewFrame;
CGRect bottomInputTextViewFrame;
CGRect bottomBgViewFrame;
CGRect bottomBuyViewFrame;
}
#pragma system method
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
returnself;
}
- (void)viewDidLoad
{
[superviewDidLoad];
[self initData];
[selfinitVariable];
[self.tableViewreloadData];
}
-(void)viewWillAppear:(BOOL)animated
{
[superviewWillDisappear:YES];
[selfinitBottomView];
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(mainSwitch:) name:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(mainSwitch:) name:UIKeyboardWillHideNotificationobject:nil];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
}
-(void)viewWillDisappear:(BOOL)animated
{
[superviewWillDisappear:YES];
[[NSNotificationCenterdefaultCenter]removeObserver:self];
}
#pragma network
#pragma textView delegate
- (void)textViewDidChange:(UITextView *)textView
{
CGSize contentSize = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, FLT_MAX)];
if (contentSize.height > kTextViewDefaultHeight) {
contentSize.height = kTextViewDefaultHeight;
bottomView.inputTextView.scrollEnabled = YES;
}else {
bottomView.inputTextView.scrollEnabled = NO;
}
if (textView.contentSize.height > textView.frame.size.height) {
textView.contentOffset = CGPointMake(textView.contentOffset.x, textView.contentSize.height - textView.frame.size.height);
}
[selfadjustTextViewHeightBysize:contentSize];
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
returnYES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
faceShowing = NO;
[bottomView.faceBtnsetImage:[UIImageimageNamed:@"face_icon.png"] forState:UIControlStateNormal];
returnYES;
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
faceShowing = YES;
[bottomView.faceBtnsetImage:[UIImageimageNamed:@"keyboard_icon.png"] forState:UIControlStateNormal];
}
#pragma tableview delegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
static NSString *mIdentifier = @"IMCelebrityCell";
IMCelebrityCell *imCell = [tableView dequeueReusableCellWithIdentifier:mIdentifier];
if (!imCell) {
imCell = [[[UINibnibWithNibName:mIdentifier bundle:nil]instantiateWithOwner:selfoptions:nil]objectAtIndex:0];
}
return imCell;
}
static NSString *identifier = @"IMChatCell";
IMChatCell *detailCell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!detailCell) {
detailCell = [[[UINibnibWithNibName:identifier bundle:nil]instantiateWithOwner:selfoptions:nil]objectAtIndex:0];
}
MessageDetailInfo *info = [self.dataArray objectAtIndex:indexPath.row];
detailCell.info = info;
return detailCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 301;
}
CGFloat rowHeight = [IMChatCell heightForMessage:[self.dataArray objectAtIndex:indexPath.row]];
return rowHeight;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
returnself.dataArray.count;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (scrollView.contentOffset.y < 0)
{
[selfreloadTableViewDataSource];
}
}
- (void)reloadTableViewDataSource
{
}
#pragma my Action
-(IBAction)keyboardAction:(id)sender
{
UIButton *btn = (UIButton *)sender;
if (btn.tag == keyboardShow) {
bottomView.inputView.hidden = YES;
bottomView.naviView.hidden = NO;
frameState = allHide;
if (![bottomView.inputTextViewisFirstResponder]) {
[self mainSwitch:keyboardNoti];
}else {
[bottomView.inputTextViewresignFirstResponder];
}
faceShowing = NO;
bottomView.boardBtn.tag = keyboardHide;
}else{
bottomView.inputView.hidden = NO;
bottomView.naviView.hidden = YES;
frameState = allShow;
[bottomView.inputTextViewbecomeFirstResponder];
bottomView.boardBtn.tag = keyboardShow;
}
}
#pragma initial work
-(void)initBottomView
{
bottomView = [[[UINibnibWithNibName:@"IMBottomView"bundle:Nil]instantiateWithOwner:selfoptions:Nil]objectAtIndex:0];
self.faceInputView.targetView = bottomView.inputTextView;
bottomView.frame = CGRectMake(0, self.contentView.frame.size.height - bottomView.frame.size.height, bottomView.frame.size.width, bottomView.frame.size.height);
[bottomView.boardBtnaddTarget:selfaction:@selector(keyboardAction:) forControlEvents:UIControlEventTouchUpInside];
[bottomView.faceBtnaddTarget:selfaction:@selector(faceAction:) forControlEvents:UIControlEventTouchUpInside];
bottomView.boardBtn.tag = keyboardHide;
bottomView.inputTextView.delegate = self;
[bottomView.inputBgImageViewsetImage:[[UIImageimageNamed:@"celebrity_input_bg.png"]stretchableImageWithLeftCapWidth:30topCapHeight:15]];
buyView = [[[UINibnibWithNibName:@"IMBuyView"bundle:Nil]instantiateWithOwner:selfoptions:Nil]objectAtIndex:0];
buyView.frame = CGRectMake(buyView.frame.origin.x, bottomView.frame.origin.y - buyView.frame.size.height, buyView.frame.size.width, buyView.frame.size.height);
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, buyView.frame.origin.y - naviHeight);
self.tableView.backgroundColor = [UIColorblueColor];
[self.contentViewinsertSubview:bottomViewaboveSubview:self.tableView];
[self.contentViewinsertSubview:buyViewaboveSubview:self.tableView];
bottomViewFrame = bottomView.frame;
bottomBgViewFrame = bottomView.inputBgImageView.frame;
bottomBuyViewFrame = buyView.frame;
bottomInputTextViewFrame = bottomView.inputTextView.frame;
}
-(void)initData
{
if (!self.dataArray) {
self.dataArray = [NSMutableArray array];
}
for (int i = 0; i< 10; i++) {
ChatMessageInfo *info = [[ChatMessageInfoalloc]init];
info.content = @"你好";
if (i%2 == 0) {
info.messageFrom = @"wangyanan";
}else{
info.messageFrom = @"wangyanan0";
}
[self.dataArray addObject:info];
}
}
-(void)initVariable
{
faceShowing = NO;
hideAll = NO;
frameState = allShow;
}
#pragma faceAction
- (IBAction)faceAction:(id)sender
{
if (faceShowing) {
frameState = boardShow;
[bottomView.inputTextViewbecomeFirstResponder];
[bottomView.faceBtnsetImage:[UIImageimageNamed:@"face_icon.png"] forState:UIControlStateNormal];
}else{
frameState = boardHide;
if (![bottomView.inputTextViewisFirstResponder]) {
[self mainSwitch:keyboardNoti];
}else{
[bottomView.inputTextViewresignFirstResponder];
}
[bottomView.faceBtnsetImage:[UIImageimageNamed:@"keyboard_icon.png"] forState:UIControlStateNormal];
}
}
#pragma handle frame
-(void)showUpperViewFrameHandler:(NSNotification *)notification
{
keyboardNoti = notification;
CGRect _keyboardRect = [[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardHeight = _keyboardRect.size.height;
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDelegate:self];
[UIViewsetAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIViewsetAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
bottomView.frame = CGRectMake(bottomView.frame.origin.x,bottomView.frame.origin.y - _keyboardRect.size.height, bottomView.frame.size.width, bottomView.frame.size.height);
buyView.frame = CGRectMake(buyView.frame.origin.x,bottomView.frame.origin.y - buyView.frame.size.height, buyView.frame.size.width, buyView.frame.size.height);
[UIViewcommitAnimations];
}
-(void)showTableViewFrameHandler:(NSNotification *)notification
{
CGRect _keyboardRect = [[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardHeight = _keyboardRect.size.height;
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDelegate:self];
[UIViewsetAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIViewsetAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y - _keyboardRect.size.height, self.tableView.frame.size.width, self.tableView.frame.size.height);
[selfadjustTableViewContentOffSetWhenShowboard];
[UIViewcommitAnimations];
}
-(void)showFaceViewFrameHandler:(NSNotification *)notification
{
CGRect _keyboardRect = [[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardHeight = _keyboardRect.size.height;
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDelegate:self];
[UIViewsetAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIViewsetAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
self.faceInputView.frame = CGRectMake(0, self.contentView.frame.size.height - self.faceInputView.frame.size.height, self.faceInputView.frame.size.width, self.faceInputView.frame.size.height);
[UIViewcommitAnimations];
}
-(void)hideUpperViewFrameHandler:(NSNotification *)notification
{
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDelegate:self];
[UIViewsetAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIViewsetAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
bottomView.frame =bottomViewFrame;
bottomView.inputBgImageView.frame = bottomBgViewFrame;
buyView.frame = bottomBuyViewFrame;
bottomView.inputTextView.frame = bottomInputTextViewFrame;
bottomView.frame = CGRectMake(bottomView.frame.origin.x, self.contentView.frame.size.height - kBottomViewHeightDefault, bottomView.frame.size.width, kBottomViewHeightDefault);
buyView.frame = CGRectMake(buyView.frame.origin.x,bottomView.frame.origin.y - buyView.frame.size.height, buyView.frame.size.width, buyView.frame.size.height);
[UIViewcommitAnimations];
}
-(void)hideTableViewFrameHandler:(NSNotification *)notification
{
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDelegate:self];
[UIViewsetAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIViewsetAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y + keyboardHeight, self.tableView.frame.size.width, self.tableView.frame.size.height);
[UIViewcommitAnimations];
}
-(void)hideFaceViewFrameHandler:(NSNotification *)notification
{
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDelegate:self];
[UIViewsetAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIViewsetAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
self.faceInputView.frame = CGRectMake(0, self.contentView.frame.size.height, self.faceInputView.frame.size.width, self.faceInputView.frame.size.height);
[UIViewcommitAnimations];
}
#pragma middle code adjust
-(void)adjustTextViewHeightBysize:(CGSize)contentSize
{
bottomView.frame = CGRectMake(bottomView.frame.origin.x, self.contentView.frame.size.height - keyboardHeight - contentSize.height - textViewBgAndCursor*2, bottomView.frame.size.width, contentSize.height + textViewBgAndCursor * 2);
CGFloat inputTextViewHeight = contentSize.height - textViewHeightLimit < 45 ? 45: (contentSize.height - textViewHeightLimit);
bottomView.inputTextView.frame = CGRectMake(bottomView.inputTextView.frame.origin.x, bottomView.inputTextView.frame.origin.y, bottomView.inputTextView.frame.size.width, inputTextViewHeight);
bottomView.inputBgImageView.frame = CGRectMake(bottomView.inputBgImageView.frame.origin.x, textViewBgToTop, bottomView.inputBgImageView.frame.size.width, contentSize.height);
buyView.frame = CGRectMake(buyView.frame.origin.x, bottomView.frame.origin.y - buyView.frame.size.height, buyView.frame.size.width, buyView.frame.size.height);
}
-(void)adjustTableViewContentOffSetWhenShowboard
{
if (self.tableView.contentSize.height > self.tableView.frame.size.height) {
}elseif(self.tableView.contentSize.height <= keyboardHeight){
self.tableView.contentInset = UIEdgeInsetsMake(keyboardHeight - (buyView.frame.size.height + bottomView.frame.size.height), self.tableView.contentInset.left, self.tableView.contentInset.bottom, self.tableView.contentInset.right);
}elseif(self.tableView.contentSize.height > keyboardHeight){
}else{
}
}
-(void)adjustTableViewContentOffSetWhenReceiveOrSendMessage
{
if (self.tableView.contentSize.height > self.tableView.frame.size.height) {
self.tableView.contentOffset = CGPointMake(self.tableView.contentOffset.x, self.tableView.contentSize.height - (self.contentView.frame.size.height - naviHeight - buyView.frame.size.height - bottomView.frame.size.height));
}
}
#pragma main switch
-(IBAction)mainSwitch:(NSNotification *)notification
{
switch (frameState) {
case allShow:
{
[self showFaceViewFrameHandler:notification];
[self showTableViewFrameHandler:notification];
[self showUpperViewFrameHandler:notification];
[selftextViewDidChange:bottomView.inputTextView];
break;
}
case allHide:
{
[self hideFaceViewFrameHandler:notification];
[self hideTableViewFrameHandler:notification];
[self hideUpperViewFrameHandler:notification];
break;
}
case boardHide:
break;
case boardShow:
break;
default:
[self showFaceViewFrameHandler:notification];
break;
}
}
@end