JXMenuView.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // JXMenuView.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 2018/9/6.
  6. // Copyright © 2018年 Reese. All rights reserved.
  7. //
  8. #import "JXMenuView.h"
  9. #import "UIImage+Color.h"
  10. #define HEIGHT 38 // 点赞评论控件整体高
  11. #define INSET 16 // 点赞评论控件左右间距
  12. #define TEXT_FONT SYSFONT(14) // 字体大小
  13. @interface JXMenuView ()
  14. @property (nonatomic, strong) NSArray *images;
  15. //@property (nonatomic, strong) UIView *baseView;
  16. @property (nonatomic, assign) CGFloat w;
  17. @property (nonatomic, assign) CGPoint point;
  18. @end
  19. @implementation JXMenuView
  20. // Point (.x 暂时无效)
  21. - (instancetype)initWithPoint:(CGPoint)point Title:(NSArray *)titles Images:(NSArray *)images {
  22. self = [super init];
  23. if (self) {
  24. self.point = point;
  25. self.images = images;
  26. self.titles = titles;
  27. [self setupViews];
  28. }
  29. return self;
  30. }
  31. - (void)setupViews {
  32. self.backgroundColor = HEXCOLOR(0x3B4042);
  33. self.layer.masksToBounds = YES;
  34. self.layer.cornerRadius = 4.0f;
  35. NSInteger w = 0;
  36. UIButton *cellView;
  37. for (int i = 0; i < self.titles.count; i++) {
  38. NSString *str = self.titles[i];
  39. CGSize size = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:TEXT_FONT} context:nil].size;
  40. cellView = [[UIButton alloc] init];
  41. UIImageView *imgV;
  42. if (self.images.count > 0 && i < self.images.count) {
  43. CGFloat H = 14.f;
  44. cellView.frame = CGRectMake(w, 0, INSET*2+H+size.width, HEIGHT);
  45. imgV = [[UIImageView alloc] initWithFrame:CGRectMake(INSET, (HEIGHT-H)/2, H, H)];
  46. imgV.image = [UIImage imageNamed:self.images[i]];
  47. [cellView addSubview:imgV];
  48. } else {
  49. cellView.frame = CGRectMake(w, 0, INSET*2+size.width, HEIGHT);
  50. }
  51. UILabel *textLabel = [[UILabel alloc] init];
  52. textLabel.font = TEXT_FONT;
  53. textLabel.text = str;
  54. textLabel.textColor = [UIColor whiteColor];
  55. if (i < self.images.count) {
  56. textLabel.frame = CGRectMake(CGRectGetMaxX(imgV.frame)+4, (HEIGHT-size.height)/2, size.width, size.height);
  57. }else {
  58. textLabel.frame = CGRectMake(0, (HEIGHT-size.height)/2, cellView.frame.size.width, size.height);
  59. textLabel.textAlignment = NSTextAlignmentCenter;
  60. }
  61. [cellView addSubview:textLabel];
  62. cellView.backgroundColor = [UIColor clearColor];
  63. cellView.tag = i;
  64. [cellView setImage:[UIImage createImageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
  65. [cellView setImage:[UIImage createImageWithColor:[UIColor blackColor]] forState:UIControlStateHighlighted];
  66. [cellView addTarget:self action:@selector(didCellView:) forControlEvents:UIControlEventTouchUpInside];
  67. [self addSubview:cellView];
  68. if (i > 0) {
  69. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 0.5, HEIGHT-10)];
  70. line.backgroundColor = HEXCOLOR(0x292D2F);
  71. [cellView addSubview:line];
  72. }
  73. w += cellView.frame.size.width;
  74. }
  75. _w = w;
  76. // 获取 回复按钮的Point (.x 暂时无效)
  77. CGRect frame = self.frame;
  78. frame.origin = self.point;
  79. // self.frame = CGRectMake(frame.origin.x+_w, frame.origin.y, 0, HEIGHT);
  80. // [UIView animateWithDuration:.1f animations:^{
  81. // self.frame = CGRectMake(frame.origin.x, frame.origin.y, w, HEIGHT);
  82. // }];
  83. self.frame = CGRectMake(JX_SCREEN_WIDTH-44, frame.origin.y, 0, HEIGHT);
  84. [UIView animateWithDuration:.1f animations:^{
  85. self.frame = CGRectMake(JX_SCREEN_WIDTH-_w-44, frame.origin.y, w, HEIGHT);
  86. }];
  87. }
  88. - (void)didCellView:(UIButton *)button {
  89. if (self.delegate && [self.delegate respondsToSelector:@selector(didMenuView:WithButtonIndex:)]) {
  90. [self dismissBaseView];
  91. [self.delegate didMenuView:self WithButtonIndex:button.tag];
  92. }
  93. }
  94. - (void)dismissBaseView {
  95. if (self) {
  96. [UIView animateWithDuration:.08f animations:^{
  97. self.frame = CGRectMake(self.frame.origin.x+_w, self.frame.origin.y, 0, HEIGHT);
  98. } completion:^(BOOL finished) {
  99. UIView *view = self;
  100. [view removeFromSuperview];
  101. view = nil;
  102. }];
  103. }
  104. }
  105. @end