PrivateChatViewController.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // PrivateChatViewController.m
  3. // BuguLive
  4. //
  5. // Created by fanwe2014 on 16/6/27.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "PrivateChatViewController.h"
  9. #import "sendMessageView.h"
  10. #define NavigationHeight 64
  11. #define buttonViewHeight 40
  12. @interface PrivateChatViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
  13. {
  14. UITableView *_myTableView;
  15. NSMutableArray *_dataArray;
  16. sendMessageView *_messageView;
  17. UIView *_buttonView;//底部视图的View
  18. UITextField *_textFiled;
  19. UIImageView *_soundImgView;
  20. UIImageView *_expressionView;
  21. UIImageView *_addImgView;
  22. CGFloat _keyheight;
  23. }
  24. @end
  25. @implementation PrivateChatViewController
  26. - (void)viewDidLoad
  27. {
  28. [super viewDidLoad];
  29. _dataArray = [[NSMutableArray alloc]init];
  30. self.view.backgroundColor = [UIColor whiteColor];
  31. self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithTarget:self action:@selector(comeBack) image:@"com_arrow_vc_back" highImage:@"com_arrow_vc_back"];
  32. _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-64-40)];
  33. _myTableView.delegate = self;
  34. _myTableView.dataSource = self;
  35. _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  36. [self.view addSubview:_myTableView];
  37. [self creatView];
  38. [self addChatToolBar];
  39. [self registerForKeyboardNotifications];
  40. UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
  41. //设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。
  42. tapGestureRecognizer.cancelsTouchesInView = NO;
  43. //将触摸事件添加到当前view
  44. [_myTableView addGestureRecognizer:tapGestureRecognizer];
  45. }
  46. - (void)keyboardHide:(UITapGestureRecognizer*)tap{
  47. [_textFiled resignFirstResponder];
  48. CGRect tabelViewRect = _myTableView.frame;
  49. tabelViewRect.origin.y = 0;
  50. _myTableView.frame = _myTableView.frame;
  51. CGRect buttonViewRect = _buttonView.frame;
  52. buttonViewRect.origin.y = kScreenH-NavigationHeight-buttonViewHeight;
  53. _buttonView.frame = buttonViewRect;
  54. }
  55. #pragma mark 键盘的显示和隐藏
  56. - (void)registerForKeyboardNotifications
  57. {
  58. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
  59. }
  60. - (void) keyboardWasShown:(NSNotification *) notif
  61. {
  62. NSDictionary *info = [notif userInfo];
  63. NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
  64. CGSize keyboardSize = [value CGRectValue].size;
  65. _keyheight = keyboardSize.height;
  66. CGRect tabelViewRect = _myTableView.frame;
  67. tabelViewRect.origin.y = -_keyheight;
  68. _myTableView.frame = _myTableView.frame;
  69. CGRect buttonViewRect = _buttonView.frame;
  70. buttonViewRect.origin.y = kScreenH-NavigationHeight-buttonViewHeight-_keyheight;
  71. _buttonView.frame = buttonViewRect;
  72. }
  73. //返回
  74. - (void)comeBack
  75. {
  76. [self.navigationController popViewControllerAnimated:YES];
  77. }
  78. #pragma mark 加页面底部视图
  79. - (void)addChatToolBar
  80. {
  81. _buttonView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenH-NavigationHeight-buttonViewHeight, kScreenW, buttonViewHeight)];
  82. _buttonView.backgroundColor = kBackGroundColor;
  83. [self.view addSubview:_buttonView];
  84. _soundImgView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 30, 30)];
  85. _soundImgView.backgroundColor = [UIColor redColor];
  86. [_buttonView addSubview:_soundImgView];
  87. _textFiled = [[UITextField alloc]initWithFrame:CGRectMake(40, 5, kScreenW-125, 30)];
  88. _textFiled.layer.borderWidth = 1;
  89. _textFiled.layer.borderColor = [UIColor lightGrayColor].CGColor;
  90. _textFiled.layer.cornerRadius = 3;
  91. [_buttonView addSubview:_textFiled];
  92. _expressionView = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW-80, 5, 30, 30)];
  93. _expressionView.backgroundColor = [UIColor redColor];
  94. [_buttonView addSubview:_expressionView];
  95. _addImgView = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW- 40, 5, 30, 30)];
  96. _addImgView.backgroundColor = [UIColor redColor];
  97. [_buttonView addSubview:_addImgView];
  98. }
  99. - (void)creatView
  100. {
  101. TIMConversation * conversation = [[TIMManager sharedInstance] getConversation:TIM_C2C receiver:self.receiverId];
  102. [conversation getLocalMessage:10 last:nil succ:^(NSArray * msgList) {
  103. for (TIMMessage * msg in msgList) {
  104. if ([msg isKindOfClass:[TIMMessage class]])
  105. {
  106. NSLog(@"GetOneMessage:%@", msg);
  107. }
  108. }
  109. }fail:^(int code, NSString * err)
  110. {
  111. NSLog(@"Get Message Failed:%d->%@", code, err);
  112. }];
  113. }
  114. #pragma mark UITableViewDataSource
  115. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  116. {
  117. return 1;
  118. }
  119. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  120. {
  121. if (_dataArray.count > 0)
  122. {
  123. return _dataArray.count;
  124. }else
  125. {
  126. return 0;
  127. }
  128. }
  129. //返回单元格
  130. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  131. {
  132. //MainLiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainLiveTableViewCell"];
  133. static NSString *reusedID = @"reusedID";
  134. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedID];
  135. if (cell == nil)
  136. {
  137. //如果找不到,就创建.并且打上复用id
  138. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedID];
  139. }
  140. cell.textLabel.text = [NSString stringWithFormat:ASLocalizedString(@"第%d行"),(int)indexPath.row];
  141. return cell;
  142. }
  143. @end