JX_SelectMenuView.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // JX_SelectMenuView.m
  3. // shiku_im
  4. //
  5. // Created by Apple on 16/9/12.
  6. // Copyright © 2016年 Reese. All rights reserved.
  7. //
  8. #import "JX_SelectMenuView.h"
  9. #import "UIImage+Color.h"
  10. #define INSET_INT 15 // 左右间隔
  11. #define IMAGE_W 17 // 图片宽高
  12. @implementation JX_SelectMenuView
  13. - (instancetype)initWithTitle:(NSArray *)titleArr image:(NSArray *)images cellHeight:(int)height {
  14. self = [super init];
  15. if (self) {
  16. int topHeight = 12;
  17. // 获取当前最长的字符串
  18. NSString *currentStr = titleArr[0];
  19. for (NSString *str in titleArr) {
  20. if (currentStr.length < str.length) {
  21. currentStr = str;
  22. }
  23. }
  24. CGSize size = [currentStr boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:SYSFONT(15)} context:nil].size;
  25. //顶部尖角高度topHeight
  26. //每格高度heigth
  27. //大小
  28. self.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  29. self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.2];
  30. UITapGestureRecognizer *bigTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideMenuView)];
  31. [self addGestureRecognizer:bigTap];
  32. UIView *bigView = [[UIView alloc] init];
  33. CGPoint point;
  34. if (images.count > 0) { // 有图标
  35. point = CGPointMake(JX_SCREEN_WIDTH - (size.width+INSET_INT*3+IMAGE_W) - 10, JX_SCREEN_TOP);
  36. bigView.frame = CGRectMake(point.x, point.y+3 , size.width+INSET_INT*3+IMAGE_W, titleArr.count *height+topHeight);
  37. } else { // 无图标
  38. point = CGPointMake(JX_SCREEN_WIDTH - (size.width + INSET_INT*2) - 10, JX_SCREEN_TOP);
  39. bigView.frame = CGRectMake(point.x, point.y+3 , size.width+INSET_INT*2, titleArr.count*height+topHeight);
  40. }
  41. [self addSubview:bigView];
  42. //背景图片
  43. UIImageView * bgImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, bigView.frame.size.width, bigView.frame.size.height)];
  44. bgImage.userInteractionEnabled = NO;
  45. UIImage *image = [[UIImage imageNamed:@"Haircircleoffriends_white"] resizableImageWithCapInsets:UIEdgeInsetsMake(25, 10, 25, 10) resizingMode:UIImageResizingModeStretch];
  46. bgImage.image = image;
  47. [bigView addSubview:bgImage];
  48. //动态设置
  49. for (int i = 0; i < [titleArr count]; i++) {
  50. UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, topHeight + height *i, bigView.frame.size.width, height)];
  51. baseView.tag = i;
  52. [bigView addSubview:baseView];
  53. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didMenuViewCell:)];
  54. [baseView addGestureRecognizer:tap];
  55. //添加分割线
  56. if (i < [titleArr count]-1) {
  57. UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(INSET_INT, baseView.frame.size.height-LINE_WH, baseView.frame.size.width-INSET_INT*2, LINE_WH)];
  58. lineView.backgroundColor = THE_LINE_COLOR;
  59. [baseView addSubview:lineView];
  60. }
  61. UILabel * titleLabel = [[UILabel alloc]init];
  62. UIImageView *imageView;
  63. if (images.count > 0 && i < images.count) { // 有图标
  64. imageView = [[UIImageView alloc] initWithFrame:CGRectMake(INSET_INT, (height-IMAGE_W)/2, IMAGE_W, IMAGE_W)];
  65. imageView.image = [UIImage imageNamed:images[i]];
  66. imageView.backgroundColor = [UIColor clearColor];
  67. [baseView addSubview:imageView];
  68. titleLabel.frame = CGRectMake(CGRectGetMaxX(imageView.frame)+INSET_INT, 0, baseView.frame.size.width-INSET_INT*3, height - LINE_WH);
  69. titleLabel.textAlignment = NSTextAlignmentLeft;
  70. } else { // 无图标
  71. titleLabel.frame = CGRectMake(INSET_INT, 0, baseView.frame.size.width-INSET_INT*2, height - LINE_WH);
  72. titleLabel.textAlignment = NSTextAlignmentCenter;
  73. }
  74. //设置标题
  75. titleLabel.text = titleArr[i];
  76. titleLabel.textColor = [UIColor blackColor];
  77. titleLabel.font = SYSFONT(15);
  78. titleLabel.userInteractionEnabled = NO;
  79. [baseView addSubview:titleLabel];
  80. }
  81. }
  82. return self;
  83. }
  84. - (void)didMenuViewCell:(UITapGestureRecognizer *)tap {
  85. if (self.delegate &&[self.delegate respondsToSelector:@selector(didMenuView:WithIndex:)]) {
  86. [self.delegate didMenuView:self WithIndex:tap.view.tag];
  87. [self hide];
  88. }
  89. }
  90. - (void)hideMenuView {
  91. [self hide];
  92. }
  93. - (void)hide {
  94. if (self) {
  95. [self removeFromSuperview];
  96. }
  97. }
  98. @end