2
0

WFCUMoreBoardView.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // WFCUMoreBoardView.m
  3. // WFChatUIKit
  4. //
  5. // Created by Rain on 2022/9/28.
  6. // Copyright © 2022 Wildfirechat. All rights reserved.
  7. //
  8. #import "WFCUMoreBoardView.h"
  9. @implementation MoreItem
  10. - (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image callback:(MoreItem *(^)(void))onClicked; {
  11. self = [super init];
  12. if (self) {
  13. self.title = title;
  14. self.image = image;
  15. self.onClicked = onClicked;
  16. }
  17. return self;
  18. }
  19. @end
  20. @interface WFCUMoreBoardView ()
  21. @property(nonatomic, strong)NSArray<MoreItem *> *items;
  22. @property(nonatomic, strong)void(^cancelBlock)(WFCUMoreBoardView *boardView);
  23. @property(nonatomic, assign)CGFloat parentWidth;
  24. @end
  25. #define ITEM_SIZE 48
  26. #define CANCEL_BTN_HEIGHT 36
  27. @implementation WFCUMoreBoardView
  28. - (instancetype)initWithWidth:(CGFloat)width items:(NSArray<MoreItem *> *)items cancel:(void(^)(WFCUMoreBoardView *boardView))cancelBlock {
  29. self = [super initWithFrame:CGRectZero];
  30. if(self) {
  31. self.items = items;
  32. self.cancelBlock = cancelBlock;
  33. self.parentWidth = width;
  34. [self setupSubViews];
  35. }
  36. return self;
  37. }
  38. - (void)setupSubViews {
  39. self.backgroundColor = [UIColor colorWithRed:0.2 green:0.37 blue:0.9 alpha:1];
  40. for (int i = 0; i < self.items.count; i++) {
  41. [self addButtonAtIndex:i];
  42. }
  43. CGFloat boardWidth = self.parentWidth - 16 - 16;
  44. int ITEM_COUNT_OF_LINE = (int)boardWidth/ITEM_SIZE/(1.5);
  45. CGFloat padding = boardWidth/ITEM_COUNT_OF_LINE - ITEM_SIZE;
  46. int line = (int)self.items.count / ITEM_COUNT_OF_LINE;
  47. UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(16, (padding+ITEM_SIZE)*(line+1), boardWidth - 32, CANCEL_BTN_HEIGHT)];
  48. [cancelBtn setTitle:WFCString(@"Cancel") forState:UIControlStateNormal];
  49. [cancelBtn addTarget:self action:@selector(onCancelBtn:) forControlEvents:UIControlEventTouchDown];
  50. cancelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  51. cancelBtn.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];
  52. cancelBtn.layer.masksToBounds = YES;
  53. cancelBtn.layer.cornerRadius = 5.f;
  54. [self addSubview:cancelBtn];
  55. self.frame = CGRectMake(16, 0, boardWidth, (line+1)*(padding+ITEM_SIZE) + CANCEL_BTN_HEIGHT + 16);
  56. self.layer.masksToBounds = YES;
  57. self.layer.cornerRadius = 10.f;
  58. }
  59. - (void)addButtonAtIndex:(int)i {
  60. CGFloat boardWidth = self.parentWidth - 16 - 16;
  61. int ITEM_COUNT_OF_LINE = (int)boardWidth/ITEM_SIZE/(1.5);
  62. CGFloat padding = boardWidth/ITEM_COUNT_OF_LINE - ITEM_SIZE;
  63. int line = i / ITEM_COUNT_OF_LINE;
  64. int row = i % ITEM_COUNT_OF_LINE;
  65. CGFloat startX = padding/2 + (ITEM_SIZE + padding) * row;
  66. CGFloat startY = padding/2 + (ITEM_SIZE + padding) * line;
  67. MoreItem *item = self.items[i];
  68. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(startX, startY, ITEM_SIZE, ITEM_SIZE)];
  69. btn.tag = i;
  70. [btn setImage:item.image forState:UIControlStateNormal];
  71. [btn setTitle:item.title forState:UIControlStateNormal];
  72. btn.titleLabel.font = [UIFont systemFontOfSize:12];
  73. btn.titleEdgeInsets = UIEdgeInsetsMake(btn.imageView.frame.size.height / 2-6, -btn.imageView.frame.size.width, -btn.imageView.frame.size.height, 0);
  74. btn.imageEdgeInsets = UIEdgeInsetsMake(-4, 0, btn.imageView.frame.size.height / 2, -btn.titleLabel.bounds.size.width);
  75. [btn addTarget:self action:@selector(onClickItem:) forControlEvents:UIControlEventTouchDown];
  76. [self addSubview:btn];
  77. }
  78. - (void)onCancelBtn:(UIButton *)btn {
  79. if(self.cancelBlock) {
  80. self.cancelBlock(self);
  81. }
  82. }
  83. - (void)onClickItem:(UIButton *)btn {
  84. MoreItem *moreItem = self.items[btn.tag].onClicked();
  85. if(moreItem) {
  86. [[self.items mutableCopy] replaceObjectAtIndex:btn.tag withObject:moreItem];
  87. [btn removeFromSuperview];
  88. [self addButtonAtIndex:(int)btn.tag];
  89. } else {
  90. [self dismiss];
  91. }
  92. }
  93. - (void)dismiss {
  94. self.cancelBlock(self);
  95. }
  96. @end