DrawerView.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 reloadData];
  51. }
  52. return self;
  53. }
  54. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  55. {
  56. // 若为UITableViewCellContentView(即点击了tableViewCell),
  57. if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
  58. // cell 不需要响应 父视图的手势,保证didselect 可以正常
  59. return NO;
  60. }
  61. //默认都需要响应
  62. return YES;
  63. }
  64. - (void)reloadDrawerData {
  65. self.drawerTitleLab.text = [NSString stringWithFormat:@"%@,您好",[AppUserModel sharedAppUserModel].Nick];
  66. _imageNameArr = @[@[@"bar_home_off", @"bar_work_off", @"bar_chat_off", @"bar_my_off"], @[@"draw_letter", @"draw_notice", @"draw_collect",@"draw_note", @"draw_set"]];
  67. _drawerNameArr = @[@[@"首页", @"工作", @"信源", @"我的"],@[ @"站内信", @"通知", @"收藏",@"笔记", @"设置"]];
  68. }
  69. #pragma mark - UITableViewDataSource && UITableViewDelegate
  70. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  71. return _imageNameArr.count;
  72. }
  73. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  74. return [(NSArray*)_imageNameArr[section] count];
  75. }
  76. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  77. return 54.f;
  78. }
  79. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  80. NSString *cellIdentifier = NSStringFromSelector(_cmd);
  81. DrawerCell *cell = (DrawerCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  82. if (!cell) {
  83. cell = [[DrawerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  84. }
  85. [cell loadCellDataWithTitle:_drawerNameArr[indexPath.section][indexPath.row] imageName:_imageNameArr[indexPath.section][indexPath.row]];
  86. return cell;
  87. }
  88. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  89. {
  90. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  91. if (self.SelectDrawerBlock) {
  92. self.SelectDrawerBlock(indexPath);
  93. }
  94. [self removeFromSuperview];
  95. }
  96. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  97. return 0.0;
  98. }
  99. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  100. return 1;
  101. }
  102. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  103. if (section == 0) {
  104. return [[UIView alloc]initWithFrame:CGRectZero];
  105. }
  106. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,tableView.width,1)];
  107. view.backgroundColor = UIColorHex(E1E1E1);
  108. return view;
  109. }
  110. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  111. return [[UIView alloc]initWithFrame:CGRectZero];
  112. }
  113. #pragma mark - setter
  114. - (UIView *)leftBgView {
  115. if (!_leftBgView) {
  116. _leftBgView = [UIView new];
  117. _leftBgView.frame = CGRectMake(0, 0, 275*Xrang, kGXScreenHeigh);
  118. _leftBgView.backgroundColor = [UIColor whiteColor];
  119. }
  120. return _leftBgView;
  121. }
  122. - (UILabel *)drawerTitleLab {
  123. if (!_drawerTitleLab) {
  124. _drawerTitleLab = [UILabel new];
  125. _drawerTitleLab.textColor = UIColorHex(232323);
  126. _drawerTitleLab.font = [UIFont systemFontOfSize:18.f];
  127. }
  128. return _drawerTitleLab;
  129. }
  130. - (UIView *)blueView {
  131. if (!_blueView) {
  132. _blueView = [UIView new];
  133. _blueView.backgroundColor = UIColorHex(336CF9);
  134. }
  135. return _blueView;
  136. }
  137. - (TDTableView *)tableView {
  138. if (!_tableView) {
  139. _tableView = [[TDTableView alloc] init];
  140. _tableView.dataSource = self;
  141. _tableView.delegate = self;
  142. }
  143. return _tableView;
  144. }
  145. @end