DrawerView.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // DrawerView.m
  3. // smartRhino
  4. //
  5. // Created by tederen on 2019/10/21.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "DrawerView.h"
  9. #import "DrawerCell.h"
  10. #define Xrang [ UIScreen mainScreen ].bounds.size.width/375 //屏幕宽比例
  11. #define Yrang [ UIScreen mainScreen ].bounds.size.height/667//屏幕高比例
  12. @interface DrawerView () <UITableViewDataSource, UITableViewDelegate,UIGestureRecognizerDelegate> {
  13. NSArray *_imageNameArr;
  14. NSArray *_drawerNameArr;
  15. }
  16. @property (nonatomic, strong) UIView *leftBgView;
  17. @property (nonatomic, strong) UILabel *drawerTitleLab;
  18. @property (nonatomic, strong) UIView *blueView;
  19. @property (nonatomic, strong) TDTableView *tableView;
  20. @end
  21. @implementation DrawerView
  22. TDShareInstance_implementation(DrawerView)
  23. - (instancetype)init{
  24. self = [super init];
  25. if (self) {
  26. self = [[DrawerView alloc]initWithFrame:CGRectMake(0, 0,kGXScreenWidth,kGXScreenHeigh - kStatusBarHeight)];
  27. [[UIApplication sharedApplication].keyWindow addSubview:self];
  28. }
  29. return self;
  30. }
  31. - (instancetype)initWithFrame:(CGRect)frame
  32. {
  33. self = [super initWithFrame:frame];
  34. if (self) {
  35. self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.65];
  36. [self addSubview:self.leftBgView];
  37. [self.leftBgView addSubview:self.drawerTitleLab];
  38. [self.leftBgView addSubview:self.blueView];
  39. [self.leftBgView addSubview:self.tableView];
  40. CGSize titleSize = [_drawerTitleLab sizeThatFits:CGSizeZero];
  41. _drawerTitleLab.frame = CGRectMake(32, kNavigationHeight, 275*Xrang - 64, titleSize.height);
  42. _blueView.frame = CGRectMake(32, kNavigationHeight+titleSize.height+4, 275*Xrang - 64, 3.f);
  43. _blueView.layer.cornerRadius = 1.5;
  44. _blueView.layer.masksToBounds = YES;
  45. _tableView.frame = CGRectMake(0,kNavigationHeight+titleSize.height+4 + 3 + 20, 275*Xrang, kGXScreenHeigh-kNavigationHeight+titleSize.height+4 + 3 + 20);
  46. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeFromSuperview)];
  47. [self addGestureRecognizer:tapGesture];
  48. tapGesture.delegate = self;
  49. [self reloadDrawerData];
  50. [self.tableView setScrollEnabled:NO];
  51. [self.tableView reloadData];
  52. }
  53. return self;
  54. }
  55. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  56. {
  57. // 若为UITableViewCellContentView(即点击了tableViewCell),
  58. if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
  59. // cell 不需要响应 父视图的手势,保证didselect 可以正常
  60. return NO;
  61. }
  62. //默认都需要响应
  63. return YES;
  64. }
  65. - (void)reloadDrawerData {
  66. self.drawerTitleLab.text = [NSString stringWithFormat:@"%@您好",ISEmptyString([AppUserModel sharedAppUserModel].Nick) ? @"" : [NSString stringWithFormat:@"%@,",[AppUserModel sharedAppUserModel].Nick]];
  67. _imageNameArr = @[@[@"draw_home", @"draw_common", @"draw_source", @"draw_mine"], @[@"draw_mail", @"draw_sixin", @"draw_collect",@"draw_note"],@[@"draw_txl", @"draw_set"]];
  68. _drawerNameArr = @[@[@"首页", @"常用", @"信源", @"我的"],@[@"收件箱", @"私信", @"收藏",@"笔记"],@[@"通讯录", @"设置"]];
  69. }
  70. #pragma mark - UITableViewDataSource && UITableViewDelegate
  71. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  72. return _imageNameArr.count;
  73. }
  74. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  75. return [(NSArray*)_imageNameArr[section] count];
  76. }
  77. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  78. return 54.f;
  79. }
  80. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  81. NSString *cellIdentifier = NSStringFromSelector(_cmd);
  82. DrawerCell *cell = (DrawerCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  83. if (!cell) {
  84. cell = [[DrawerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  85. }
  86. [cell loadCellDataWithTitle:_drawerNameArr[indexPath.section][indexPath.row] imageName:_imageNameArr[indexPath.section][indexPath.row]];
  87. return cell;
  88. }
  89. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  90. {
  91. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  92. if (self.SelectDrawerBlock) {
  93. self.SelectDrawerBlock(indexPath);
  94. }
  95. [self removeFromSuperview];
  96. }
  97. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  98. return 0.0;
  99. }
  100. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  101. return 1;
  102. }
  103. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  104. if (section == 0) {
  105. return [[UIView alloc]initWithFrame:CGRectZero];
  106. }
  107. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,tableView.width,1)];
  108. view.backgroundColor = UIColorHex(E1E1E1);
  109. return view;
  110. }
  111. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  112. return [[UIView alloc]initWithFrame:CGRectZero];
  113. }
  114. #pragma mark - setter
  115. - (UIView *)leftBgView {
  116. if (!_leftBgView) {
  117. _leftBgView = [UIView new];
  118. _leftBgView.frame = CGRectMake(0, 0, 275*Xrang, kGXScreenHeigh);
  119. _leftBgView.backgroundColor = [UIColor whiteColor];
  120. }
  121. return _leftBgView;
  122. }
  123. - (UILabel *)drawerTitleLab {
  124. if (!_drawerTitleLab) {
  125. _drawerTitleLab = [UILabel new];
  126. _drawerTitleLab.textColor = UIColorHex(232323);
  127. _drawerTitleLab.font = [UIFont systemFontOfSize:18.f];
  128. }
  129. return _drawerTitleLab;
  130. }
  131. - (UIView *)blueView {
  132. if (!_blueView) {
  133. _blueView = [UIView new];
  134. _blueView.backgroundColor = UIColorHex(0x3979D3);
  135. }
  136. return _blueView;
  137. }
  138. - (TDTableView *)tableView {
  139. if (!_tableView) {
  140. _tableView = [[TDTableView alloc] init];
  141. _tableView.dataSource = self;
  142. _tableView.delegate = self;
  143. }
  144. return _tableView;
  145. }
  146. @end