EMSearchViewController.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // EMSearchViewController.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/1/16.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import "EMSearchViewController.h"
  9. @interface EMSearchViewController ()
  10. @end
  11. @implementation EMSearchViewController
  12. - (void)viewDidLoad {
  13. [super viewDidLoad];
  14. // Do any additional setup after loading the view.
  15. self.searchBar = [[EMSearchBar alloc] init];
  16. self.searchBar.delegate = self;
  17. [self.view addSubview:self.searchBar];
  18. [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
  19. make.top.equalTo(self.view);
  20. make.left.equalTo(self.view);
  21. make.right.equalTo(self.view);
  22. make.height.equalTo(@50);
  23. }];
  24. self.tableView.rowHeight = 60;
  25. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  26. make.top.equalTo(self.searchBar.mas_bottom);
  27. make.left.equalTo(self.view);
  28. make.right.equalTo(self.view);
  29. make.bottom.equalTo(self.view);
  30. }];
  31. self.searchResults = [[NSMutableArray alloc] init];
  32. self.searchResultTableView = [[UITableView alloc] init];
  33. self.searchResultTableView.tableFooterView = [[UIView alloc] init];
  34. self.searchResultTableView.rowHeight = self.tableView.rowHeight;
  35. self.searchResultTableView.delegate = self;
  36. self.searchResultTableView.dataSource = self;
  37. }
  38. - (void)dealloc
  39. {
  40. [[EMRealtimeSearch shared] realtimeSearchStop];
  41. [[NSNotificationCenter defaultCenter] removeObserver:self];
  42. }
  43. #pragma mark - EMSearchBarDelegate
  44. - (void)searchBarShouldBeginEditing:(EMSearchBar *)searchBar
  45. {
  46. if (!self.isSearching) {
  47. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  48. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  49. self.isSearching = YES;
  50. [self.view addSubview:self.searchResultTableView];
  51. [self.searchResultTableView mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.top.equalTo(self.searchBar.mas_bottom);
  53. make.left.equalTo(self.view);
  54. make.right.equalTo(self.view);
  55. make.bottom.equalTo(self.view);
  56. }];
  57. }
  58. }
  59. - (void)searchBarCancelButtonAction:(EMSearchBar *)searchBar
  60. {
  61. [[EMRealtimeSearch shared] realtimeSearchStop];
  62. [[NSNotificationCenter defaultCenter] removeObserver:self];
  63. self.isSearching = NO;
  64. [self.searchResults removeAllObjects];
  65. [self.searchResultTableView reloadData];
  66. [self.searchResultTableView removeFromSuperview];
  67. }
  68. - (void)searchBarSearchButtonClicked:(EMSearchBar *)searchBar
  69. {
  70. }
  71. - (void)searchTextDidChangeWithString:(NSString *)aString
  72. {
  73. }
  74. #pragma mark - KeyBoard
  75. - (void)keyBoardWillShow:(NSNotification *)note
  76. {
  77. if (!self.isSearching) {
  78. return;
  79. }
  80. // 获取用户信息
  81. NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
  82. // 获取键盘高度
  83. CGRect keyBoardBounds = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  84. CGFloat keyBoardHeight = keyBoardBounds.size.height;
  85. // 获取键盘动画时间
  86. CGFloat animationTime = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
  87. // 定义好动作
  88. void (^animation)(void) = ^void(void) {
  89. [self.searchResultTableView mas_updateConstraints:^(MASConstraintMaker *make) {
  90. make.bottom.equalTo(self.view.mas_bottom).offset(-keyBoardHeight);
  91. }];
  92. };
  93. if (animationTime > 0) {
  94. [UIView animateWithDuration:animationTime animations:animation];
  95. } else {
  96. animation();
  97. }
  98. }
  99. - (void)keyBoardWillHide:(NSNotification *)note
  100. {
  101. if (!self.isSearching) {
  102. return;
  103. }
  104. // 获取用户信息
  105. NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
  106. // 获取键盘动画时间
  107. CGFloat animationTime = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
  108. // 定义好动作
  109. void (^animation)(void) = ^void(void) {
  110. [self.searchResultTableView mas_updateConstraints:^(MASConstraintMaker *make) {
  111. make.bottom.equalTo(self.view.mas_bottom);
  112. }];
  113. };
  114. if (animationTime > 0) {
  115. [UIView animateWithDuration:animationTime animations:animation];
  116. } else {
  117. animation();
  118. }
  119. }
  120. @end