JXActionSheetVC.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // JXActionSheetVC.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 2018/9/3.
  6. // Copyright © 2018年 Reese. All rights reserved.
  7. //
  8. #import "JXActionSheetVC.h"
  9. #define HEIGHT 57 // 每个成员高度,如果更改记得更改button按钮的imageEdgeInsets
  10. #define IMAGWE_W 25 // 图片宽高
  11. #define INSET 17 // 文字和图片的间距
  12. @interface JXActionSheetVC ()
  13. @property (nonatomic, strong) UIView *baseView;
  14. @property (nonatomic, strong) NSArray *names;
  15. @property (nonatomic, strong) NSArray *images;
  16. @property (nonatomic, assign) CGFloat maxX;
  17. @end
  18. @implementation JXActionSheetVC
  19. - (instancetype)initWithImages:(NSArray *)images names:(NSArray *)names {
  20. self = [super init];
  21. if (self) {
  22. //这句话是让控制器透明
  23. self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
  24. self.backGroundColor = [UIColor whiteColor];
  25. self.names = names;
  26. self.images = images;
  27. // 获取当前最长的字符串
  28. NSString *currentStr = names[0];
  29. for (NSString *str in names) {
  30. if (currentStr.length < str.length) {
  31. currentStr = str;
  32. }
  33. }
  34. // 获取最长文字的size
  35. CGSize size = [currentStr boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:SYSFONT(15)} context:nil].size;
  36. self.maxX = (JX_SCREEN_WIDTH-(size.width+INSET+IMAGWE_W))/2;
  37. }
  38. return self;
  39. }
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. self.view.tag = self.tag;
  43. self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
  44. if (self.images.count > 0 || self.names.count > 0) {
  45. self.baseView = [[UIView alloc] init];
  46. self.baseView.frame = CGRectMake(0, JX_SCREEN_HEIGHT, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  47. [self.view addSubview:self.baseView];
  48. [self setupViews];
  49. }
  50. }
  51. - (void)viewWillAppear:(BOOL)animated {
  52. [super viewWillAppear:animated];
  53. }
  54. - (void)viewDidAppear:(BOOL)animated {
  55. [super viewDidAppear:animated];
  56. [UIView animateWithDuration:.3f animations:^{
  57. self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
  58. self.baseView.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  59. }];
  60. }
  61. - (void)setupViews {
  62. // 计算取消按钮的高度
  63. CGFloat cH = THE_DEVICE_HAVE_HEAD ? JX_SCREEN_BOTTOM : HEIGHT;
  64. // 创建一个取消按钮
  65. [self createButtonWithFrame:CGRectMake(0, JX_SCREEN_HEIGHT-cH, JX_SCREEN_WIDTH, cH) index:10000];
  66. for (int i = 0; i < self.names.count; i++) {
  67. int h = HEIGHT*(i+1);
  68. // 创建成员按钮
  69. [self createButtonWithFrame:CGRectMake(0, JX_SCREEN_HEIGHT-cH - h, JX_SCREEN_WIDTH,(i == self.names.count-1) ? HEIGHT+12 : HEIGHT) index:i];
  70. }
  71. }
  72. - (void)didButton:(UIButton *)button {
  73. //离开界面
  74. [self dismissViewController];
  75. if (button.tag >= 0 && button.tag != 10000) {
  76. if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:didButtonWithIndex:)]) {
  77. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  78. [self.delegate actionSheet:self didButtonWithIndex:button.tag];
  79. });
  80. }
  81. } else {
  82. }
  83. }
  84. - (void)createButtonWithFrame:(CGRect)frame index:(int)index {
  85. UIButton *button = [[UIButton alloc] init];
  86. button.frame = frame;
  87. button.backgroundColor = self.backGroundColor;
  88. button.tag = index;
  89. [button addTarget:self action:@selector(didButton:) forControlEvents:UIControlEventTouchUpInside];
  90. [self.baseView addSubview:button];
  91. UILabel *label = [[UILabel alloc] init];
  92. UIImageView *imgV;
  93. if (self.images.count > 0 && index !=10000 && index < self.images.count) {
  94. imgV = [[UIImageView alloc] initWithFrame:CGRectMake(self.maxX, (HEIGHT-IMAGWE_W)/2, IMAGWE_W, IMAGWE_W)];
  95. imgV.image = [UIImage imageNamed:self.images[index]];
  96. [button addSubview:imgV];
  97. label.frame = CGRectMake(CGRectGetMaxX(imgV.frame)+INSET, (HEIGHT-20)/2, JX_SCREEN_WIDTH-CGRectGetMaxX(imgV.frame)-INSET, 20);
  98. }else {
  99. label.frame = CGRectMake(0, (HEIGHT-20)/2, JX_SCREEN_WIDTH, 20);
  100. label.textAlignment = NSTextAlignmentCenter;
  101. }
  102. label.backgroundColor = [UIColor clearColor];
  103. label.text = index==10000 ? Localized(@"JX_Cencal") : self.names[index];
  104. [button addSubview:label];
  105. if (index == self.names.count-1) {
  106. // 最顶部一行的位置处理
  107. button.frame = CGRectMake(button.frame.origin.x, button.frame.origin.y-12, button.frame.size.width, button.frame.size.height);
  108. imgV.frame = CGRectMake(imgV.frame.origin.x, (HEIGHT-IMAGWE_W)/2+12, imgV.frame.size.width, imgV.frame.size.height);
  109. label.frame = CGRectMake(label.frame.origin.x, (HEIGHT-20)/2+12, label.frame.size.width, label.frame.size.height);
  110. [self setPartRoundWithView:button corners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadius:17];
  111. }
  112. if (index == 10000) {
  113. //取消按钮位置处理
  114. button.frame = CGRectMake(button.frame.origin.x, button.frame.origin.y, button.frame.size.width,THE_DEVICE_HAVE_HEAD ? button.frame.size.height : HEIGHT);
  115. if (THE_DEVICE_HAVE_HEAD) { // iPhoneX 字体显示上移
  116. label.frame = CGRectMake(label.frame.origin.x, (HEIGHT-20)/2, label.frame.size.width, label.frame.size.height);
  117. }
  118. }
  119. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(33, button.frame.size.height-LINE_WH, JX_SCREEN_WIDTH-66, LINE_WH)];
  120. line.backgroundColor = THE_LINE_COLOR;
  121. [button addSubview:line];
  122. }
  123. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  124. [self dismissViewController];
  125. }
  126. - (void)dismissViewController {
  127. [UIView animateWithDuration:.3f animations:^{
  128. self.baseView.frame = CGRectMake(0, JX_SCREEN_HEIGHT, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  129. self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
  130. } completion:^(BOOL finished) {
  131. [self dismissViewControllerAnimated:YES completion:nil];
  132. if (self) {
  133. [self.view removeFromSuperview];
  134. }
  135. }];
  136. }
  137. // 画圆角
  138. - (void)setPartRoundWithView:(UIView *)view corners:(UIRectCorner)corners cornerRadius:(float)cornerRadius {
  139. CAShapeLayer *shapeLayer = [CAShapeLayer layer];
  140. shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)].CGPath;
  141. view.layer.mask = shapeLayer;
  142. }
  143. @end