2
0

WFCUPublicMenuButton.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // WFCUPublicMenuButton.m
  3. // WFChatUIKit
  4. //
  5. // Created by Rain on 2022/8/11.
  6. // Copyright © 2022 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUPublicMenuButton.h"
  9. #import "WFCUImage.h"
  10. #import "WFCUConfigManager.h"
  11. @interface WFCUPublicMenuButton ()
  12. @property(nonatomic, strong)NSMutableArray *subMenuViews;
  13. @property(nonatomic, assign)BOOL isSubMenu;
  14. @property(nonatomic, strong)UIView *subMenuContainer;
  15. @end
  16. @implementation WFCUPublicMenuButton
  17. - (instancetype)initWithFrame:(CGRect)frame {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. [self addTarget:self action:@selector(onclicked:) forControlEvents:UIControlEventTouchUpInside];
  21. self.expended = NO;
  22. self.isSubMenu = NO;
  23. self.subMenuViews = [[NSMutableArray alloc] init];
  24. self.titleLabel.font = [UIFont systemFontOfSize:16];
  25. }
  26. return self;
  27. }
  28. - (void)onclicked:(id)sender {
  29. self.expended = !self.expended;
  30. if ([self.delegate respondsToSelector:@selector(didTapButton:menu:)]) {
  31. [self.delegate didTapButton:self menu:self.channelMenu];
  32. }
  33. }
  34. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
  35. UIView *view = [super hitTest:point withEvent:event];
  36. if (self.isSubMenu) {
  37. return view;
  38. }
  39. if (view == nil){
  40. for(WFCUPublicMenuButton *subMenuView in self.subMenuViews) {
  41. //转换坐标
  42. CGPoint tempPoint = [subMenuView convertPoint:point fromView:self];
  43. //判断点击的点是否在按钮区域内
  44. if (CGRectContainsPoint(subMenuView.bounds, tempPoint)){
  45. //返回按钮
  46. return subMenuView;
  47. }
  48. }
  49. }
  50. return view;
  51. }
  52. - (void)setExpended:(BOOL)expended {
  53. _expended = expended;
  54. if (!self.channelMenu.subMenus.count) {
  55. return;
  56. }
  57. if (expended) {
  58. CGRect parentRect = self.bounds;
  59. CGPoint temPoint = [self convertPoint:CGPointMake(parentRect.size.width, 0) toView:self.superview.superview];
  60. BOOL rightEdge = temPoint.x >= [UIScreen mainScreen].bounds.size.width - 3;
  61. self.subMenuContainer = [[UIView alloc] initWithFrame:CGRectMake(rightEdge ? -16 : -8, -1 * (self.channelMenu.subMenus.count * parentRect.size.height) - self.channelMenu.subMenus.count + 1, parentRect.size.width+8, self.channelMenu.subMenus.count*parentRect.size.height+self.channelMenu.subMenus.count-1)];
  62. self.backgroundColor = [UIColor whiteColor];
  63. self.subMenuContainer.layer.shadowColor = [UIColor blackColor].CGColor;
  64. self.subMenuContainer.layer.shadowOpacity = 0.8f;
  65. [self addSubview:self.subMenuContainer];
  66. for (int i = 0; i < self.channelMenu.subMenus.count; i++) {
  67. WFCCChannelMenu *subMenu = self.channelMenu.subMenus[i];
  68. WFCUPublicMenuButton *menuBtn = [[WFCUPublicMenuButton alloc] initWithFrame:CGRectMake(0, i * parentRect.size.height + i - 1, parentRect.size.width+16, parentRect.size.height)];
  69. [menuBtn setChannelMenu:subMenu isSubMenu:YES];
  70. menuBtn.delegate = self.delegate;
  71. [self.subMenuContainer addSubview:menuBtn];
  72. [self.subMenuViews addObject:menuBtn];
  73. if (i != self.channelMenu.subMenus.count - 1) {
  74. UIView *splitbg = [[UIView alloc] initWithFrame:CGRectMake(0, (i+1) * parentRect.size.height + i - 1, parentRect.size.width+16, 1)];
  75. splitbg.backgroundColor = self.backgroundColor;
  76. [self.subMenuContainer addSubview:splitbg];
  77. UIView *split = [[UIView alloc] initWithFrame:CGRectMake(8, (i+1) * parentRect.size.height + i - 1, parentRect.size.width, 1)];
  78. split.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.9];
  79. [self.subMenuContainer addSubview:split];
  80. }
  81. }
  82. } else {
  83. [self.subMenuContainer removeFromSuperview];
  84. self.subMenuContainer = nil;
  85. }
  86. }
  87. - (void)setChannelMenu:(WFCCChannelMenu *)channelMenu isSubMenu:(BOOL)isSubMenu {
  88. self.channelMenu = channelMenu;
  89. self.isSubMenu = isSubMenu;
  90. [self setTitle:channelMenu.name forState:UIControlStateNormal];
  91. [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  92. if (!isSubMenu && channelMenu.subMenus.count) {
  93. [self setImage:[WFCUImage imageNamed:@"sub_menu"] forState:UIControlStateNormal];
  94. }
  95. if (isSubMenu) {
  96. self.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  97. // self.backgroundColor = [UIColor whiteColor];
  98. }
  99. }
  100. @end