SideSlipFilterController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // SideSlipFilterController.m
  3. // IBOSSMAP
  4. //
  5. // Created by apple on 2017/4/1.
  6. // Copyright © 2017年 apple. All rights reserved.
  7. //
  8. #import "SideSlipFilterController.h"
  9. #import "SideSlipModel.h"
  10. #import "SideSlipBaseTableViewCell.h"
  11. #import "objc/message.h"
  12. #import "SideSlipConfig.h"
  13. #import "UIColor+hexColor.h"
  14. #import "objc/runtime.h"
  15. #define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
  16. #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
  17. #define SLIP_ORIGIN_FRAME CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH - _sideSlipLeading, SCREEN_HEIGHT)
  18. #define SLIP_DISTINATION_FRAME CGRectMake(_sideSlipLeading, rectStatusHeight, SCREEN_WIDTH - _sideSlipLeading, SCREEN_HEIGHT-rectStatusHeight-[Util obtainHomeAreaHeight])
  19. const CGFloat ANIMATION_DURATION_DEFAULT = 0.3f;
  20. const CGFloat SIDE_SLIP_LEADING_DEFAULT = 20;
  21. id (*objc_msgSendGetCellIdentifier)(id self, SEL _cmd) = (void *)objc_msgSend;
  22. CGFloat (*objc_msgSendGetCellHeight)(id self, SEL _cmd) = (void *)objc_msgSend;
  23. id (*objc_msgSendCreateCellWithIndexPath)(id self, SEL _cmd, NSIndexPath *) = (void *)objc_msgSend;
  24. @interface SideSlipFilterController ()<UITableViewDelegate, UITableViewDataSource, SideSlipBaseTableViewCellDelegate>
  25. @property (copy, nonatomic) SideSlipFilterCommitBlock commitBlock;
  26. @property (copy, nonatomic) SideSlipFilterResetBlock resetBlock;
  27. @property (weak, nonatomic) UINavigationController *filterNavigation;
  28. @property (strong, nonatomic) UITableView *mainTableView;
  29. @property (strong, nonatomic) UIView *backCover;
  30. @property (weak, nonatomic) UIViewController *sponsor;
  31. @property (strong, nonatomic) NSMutableDictionary *templateCellDict;
  32. @end
  33. @implementation SideSlipFilterController
  34. - (instancetype)initWithSponsor:(UIViewController *)sponsor
  35. resetBlock:(SideSlipFilterResetBlock)resetBlock
  36. commitBlock:(SideSlipFilterCommitBlock)commitBlock {
  37. self = [super init];
  38. if (self) {
  39. _sponsor = sponsor;
  40. _resetBlock = resetBlock;
  41. _commitBlock = commitBlock;
  42. UINavigationController *filterNavigation = [[NSClassFromString(FILTER_NAVIGATION_CONTROLLER_CLASS) alloc] initWithRootViewController:self];
  43. [filterNavigation setNavigationBarHidden:YES];
  44. filterNavigation.navigationBar.translucent = NO;
  45. [filterNavigation.view setFrame:SLIP_ORIGIN_FRAME];
  46. self.filterNavigation = filterNavigation;
  47. [self configureStatic];
  48. [self configureUI];
  49. }
  50. return self;
  51. }
  52. - (void)viewDidLoad {
  53. [super viewDidLoad];
  54. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  55. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  56. }
  57. #pragma mark 键盘出现
  58. -(void)keyboardWillShow:(NSNotification *)note
  59. {
  60. CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  61. self.mainTableView.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0);
  62. }
  63. #pragma mark 键盘消失
  64. -(void)keyboardWillHide:(NSNotification *)note
  65. {
  66. self.mainTableView.contentInset = UIEdgeInsetsZero;
  67. }
  68. - (void)configureUI {
  69. //bottomView
  70. UIView *bottomView = [self createBottomView];
  71. [self.view addSubview:bottomView];
  72. UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
  73. headView.backgroundColor = [UIColor whiteColor];
  74. [headView setTranslatesAutoresizingMaskIntoConstraints:NO];
  75. UIButton *btnHead=[UIButton buttonWithType:UIButtonTypeCustom];
  76. btnHead.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  77. btnHead.frame= CGRectMake(SCREEN_WIDTH - 100, 0, 80, 40);
  78. btnHead.titleLabel.font= [UIFont systemFontOfSize:14];
  79. [btnHead setTitle:@"取消" forState:UIControlStateNormal];
  80. [btnHead setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  81. [btnHead addTarget:self action:@selector(clickBackCover:) forControlEvents:UIControlEventTouchUpInside];
  82. [headView addSubview:btnHead];
  83. [self.view addSubview:headView];
  84. NSDictionary *views = @{@"headView":headView,@"mainTableView":self.mainTableView, @"bottomView":bottomView};
  85. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:bottomView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.f constant:BOTTOM_BUTTON_HEIGHT]];
  86. [self.view addConstraint:[NSLayoutConstraint constraintWithItem:headView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.f constant:40]];
  87. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[headView]|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]];
  88. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mainTableView]|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]];
  89. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[bottomView]|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]];
  90. [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[headView][mainTableView][bottomView]|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]];
  91. }
  92. - (void)configureStatic {
  93. self.animationDuration = ANIMATION_DURATION_DEFAULT;
  94. self.sideSlipLeading = SIDE_SLIP_LEADING_DEFAULT;
  95. }
  96. - (void)show {
  97. [_sponsor.navigationController.view addSubview:self.backCover];
  98. [_sponsor.navigationController addChildViewController:self.navigationController];
  99. [_sponsor.navigationController.view addSubview:self.navigationController.view];
  100. _sponsor.navigationController.interactivePopGestureRecognizer.enabled = NO;
  101. [_backCover setHidden:YES];
  102. [UIView animateWithDuration:_animationDuration animations:^{
  103. [self.navigationController.view setFrame:SLIP_DISTINATION_FRAME];
  104. } completion:^(BOOL finished) {
  105. [_backCover setHidden:NO];
  106. }];
  107. }
  108. -(void)showPagerView:(UINavigationController*)nav{
  109. [nav.view addSubview:self.backCover];
  110. [nav addChildViewController:self.navigationController];
  111. [nav.view addSubview:self.navigationController.view];
  112. nav.interactivePopGestureRecognizer.enabled = NO;
  113. [_backCover setHidden:YES];
  114. [UIView animateWithDuration:_animationDuration animations:^{
  115. [self.navigationController.view setFrame:SLIP_DISTINATION_FRAME];
  116. } completion:^(BOOL finished) {
  117. [_backCover setHidden:NO];
  118. }];
  119. }
  120. - (void)dismiss {
  121. [UIView animateWithDuration:_animationDuration animations:^{
  122. [self.navigationController.view setFrame:SLIP_ORIGIN_FRAME];
  123. } completion:^(BOOL finished) {
  124. [_backCover removeFromSuperview];
  125. [self.navigationController.view removeFromSuperview];
  126. [self.navigationController removeFromParentViewController];
  127. }];
  128. }
  129. - (UIView *)createBottomView {
  130. UIView *bottomView = [[UIView alloc] init];
  131. [bottomView setTranslatesAutoresizingMaskIntoConstraints:NO];
  132. //resetButton
  133. UIButton *resetButton = [[UIButton alloc] init];
  134. [resetButton setTranslatesAutoresizingMaskIntoConstraints:NO];
  135. [resetButton addTarget:self action:@selector(clickResetButton:) forControlEvents:UIControlEventTouchUpInside];
  136. [resetButton.titleLabel setFont:[UIFont systemFontOfSize:BOTTOM_BUTTON_FONT_SIZE]];
  137. [resetButton setTitleColor:[UIColor hexColor:FILTER_BLACK_STRING] forState:UIControlStateNormal];
  138. // NSString *resetString = LocalString(@"sZYFilterReset");
  139. // if ([resetString isEqualToString:@"sZYFilterReset"]) {
  140. // resetString = @"Reset";
  141. // }
  142. [resetButton setTitle:@"重置" forState:UIControlStateNormal];
  143. [resetButton setBackgroundColor:[UIColor whiteColor]];
  144. [bottomView addSubview:resetButton];
  145. //commitButton
  146. UIButton *commitButton = [[UIButton alloc] init];
  147. [commitButton setTranslatesAutoresizingMaskIntoConstraints:NO];
  148. [commitButton addTarget:self action:@selector(clickCommitButton:) forControlEvents:UIControlEventTouchUpInside];
  149. [commitButton.titleLabel setFont:[UIFont systemFontOfSize:BOTTOM_BUTTON_FONT_SIZE]];
  150. [commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  151. // NSString *commitString = LocalString(@"sZYFilterCommit");
  152. // if ([commitString isEqualToString:@"sZYFilterCommit"]) {
  153. // commitString = @"Commit";
  154. // }
  155. [commitButton setTitle:@"确定" forState:UIControlStateNormal];
  156. [commitButton setBackgroundColor:[UIColor hexColor:FILTER_RED_STRING]];
  157. [bottomView addSubview:commitButton];
  158. //constraints
  159. NSDictionary *views = NSDictionaryOfVariableBindings(resetButton, commitButton);
  160. [bottomView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[resetButton][commitButton]|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];
  161. [bottomView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[resetButton]|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];
  162. [bottomView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[commitButton]|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];
  163. [bottomView addConstraint:[NSLayoutConstraint constraintWithItem:resetButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:commitButton attribute:NSLayoutAttributeWidth multiplier:1.f constant:0.f]];
  164. return bottomView;
  165. }
  166. - (void)clickResetButton:(id)sender {
  167. _resetBlock(_dataList);
  168. [[NSNotificationCenter defaultCenter] postNotificationName:FILTER_NOTIFICATION_NAME_DID_RESET_DATA object:nil];
  169. [_mainTableView reloadData];
  170. }
  171. - (void)clickCommitButton:(id)sender {
  172. _commitBlock(_dataList);
  173. [[NSNotificationCenter defaultCenter] postNotificationName:FILTER_NOTIFICATION_NAME_DID_COMMIT_DATA object:nil];
  174. //[self dismiss];
  175. }
  176. - (void)clickBackCover:(id)sender {
  177. //NSLog(@"clickBackCover");
  178. [self dismiss];
  179. }
  180. - (void)reloadData {
  181. if (_mainTableView) {
  182. [_mainTableView reloadData];
  183. }
  184. }
  185. #pragma mark - DataSource Delegate
  186. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  187. return _dataList.count;
  188. }
  189. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  190. SideSlipModel *model = _dataList[indexPath.row];
  191. Class cellClazz = NSClassFromString(model.containerCellClass);
  192. if ([(id)cellClazz respondsToSelector:@selector(cellHeight)]) {
  193. CGFloat cellHeight = objc_msgSendGetCellHeight(cellClazz, NSSelectorFromString(@"cellHeight"));
  194. return cellHeight;
  195. }
  196. NSString *identifier = objc_msgSendGetCellIdentifier(cellClazz, NSSelectorFromString(@"cellReuseIdentifier"));
  197. SideSlipBaseTableViewCell *templateCell = [self.templateCellDict objectForKey:identifier];
  198. if (!templateCell) {
  199. templateCell = objc_msgSendCreateCellWithIndexPath(cellClazz, NSSelectorFromString(@"createCellWithIndexPath:"), indexPath);
  200. templateCell.delegate = self;
  201. [self.templateCellDict setObject:templateCell forKey:identifier];
  202. }
  203. [templateCell updateFrame:self.view.frame.size.width];
  204. //update
  205. [templateCell updateCellWithModel:&model indexPath:indexPath];
  206. //calculate
  207. NSLayoutConstraint *calculateCellConstraint = [NSLayoutConstraint constraintWithItem:templateCell.contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.f constant:self.view.bounds.size.width];
  208. [templateCell.contentView addConstraint:calculateCellConstraint];
  209. CGSize cellSize = [templateCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
  210. [templateCell.contentView removeConstraint:calculateCellConstraint];
  211. CGFloat height = 0.f;
  212. if (cellSize.height>0) {
  213. height = cellSize.height;
  214. }else{
  215. height = templateCell.cellHeight;
  216. }
  217. return height;
  218. }
  219. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  220. SideSlipModel *model = _dataList[indexPath.row];
  221. Class cellClazz = NSClassFromString(model.containerCellClass);
  222. NSString *identifier = objc_msgSendGetCellIdentifier(cellClazz, NSSelectorFromString(@"cellReuseIdentifier"));
  223. SideSlipBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  224. if (!cell) {
  225. cell = objc_msgSendCreateCellWithIndexPath(cellClazz, NSSelectorFromString(@"createCellWithIndexPath:"), indexPath);
  226. cell.delegate = self;
  227. }
  228. cell.selectionStyle=UITableViewCellSelectionStyleNone;
  229. //update
  230. [cell updateCellWithModel:&model indexPath:indexPath];
  231. return cell;
  232. }
  233. - (void)sideSlipTableViewCellNeedsReload:(NSIndexPath *)indexPath {
  234. [_mainTableView reloadData];
  235. }
  236. - (void)sideSlipTableViewCellNeedsPushViewController:(UIViewController *)viewController animated:(BOOL)animated {
  237. [self.navigationController pushViewController:viewController animated:animated];
  238. }
  239. - (void)sideSlipTableViewCellNeedsDismissViewController:(UIViewController *)viewController animated:(BOOL)animated{
  240. [self presentViewController:viewController animated:NO completion:nil];
  241. }
  242. - (void)sideSlipTableViewCellNeedsScrollToCell:(UITableViewCell *)cell atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated {
  243. NSIndexPath *indexPath = [_mainTableView indexPathForRowAtPoint:cell.center];
  244. [_mainTableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
  245. }
  246. - (void)didReceiveMemoryWarning {
  247. [super didReceiveMemoryWarning];
  248. }
  249. #pragma mark - GetSet
  250. - (UITableView *)mainTableView {
  251. if (!_mainTableView) {
  252. _mainTableView = [[UITableView alloc] init];
  253. _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  254. _mainTableView.delegate = self;
  255. _mainTableView.dataSource = self;
  256. [_mainTableView setTranslatesAutoresizingMaskIntoConstraints:NO];
  257. [self.view addSubview:_mainTableView];
  258. //if (self.hasHeadView) {
  259. //_mainTableView.tableHeaderView = headView;
  260. //}
  261. }
  262. return _mainTableView;
  263. }
  264. - (NSMutableDictionary *)templateCellDict {
  265. if (!_templateCellDict) {
  266. _templateCellDict = [NSMutableDictionary dictionary];
  267. }
  268. return _templateCellDict;
  269. }
  270. - (UIView *)backCover {
  271. if (!_backCover) {
  272. _backCover = [[UIView alloc] initWithFrame:CGRectMake(0, rectStatusHeight, SCREEN_WIDTH, SCREEN_HEIGHT-rectStatusHeight)];
  273. [_backCover setBackgroundColor:[UIColor hexColor:FILTER_BACKGROUND_COVER_COLOR]];
  274. [_backCover setAlpha:FILTER_BACKGROUND_COVER_ALPHA];
  275. [_backCover addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBackCover:)]];
  276. }
  277. return _backCover;
  278. }
  279. - (UINavigationController *)filterNavigation {
  280. return objc_getAssociatedObject(_sponsor, _cmd);
  281. }
  282. - (void)setFilterNavigation:(UINavigationController *)filterNavigation {
  283. //让sponsor持有filterNavigation
  284. objc_setAssociatedObject(_sponsor, @selector(filterNavigation), filterNavigation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  285. }
  286. - (void)setDataList:(NSArray *)dataList {
  287. _dataList = [dataList copy];
  288. if (_mainTableView) {
  289. [_mainTableView reloadData];
  290. }
  291. }
  292. @end