ZCShareSheetView.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // ZCShareSheetView.m
  3. // XLChat
  4. //
  5. // Created by 张灿 on 2017/11/15.
  6. // Copyright © 2017年 张灿. All rights reserved.
  7. //
  8. #import "ZCShareSheetView.h"
  9. #define ZCShareSheetBtnHeight (40*KScreenWidth/320)//根据比例得到高度
  10. @implementation ZCShareActionBtn
  11. + (ZCShareActionBtn *)actionWithTitle:(NSString *)title image:(UIImage*)image andblock:(shareBtnBlock)block{
  12. ZCShareActionBtn *action = [[ZCShareActionBtn alloc]initWithTitle:title image:image andblock:block];
  13. return action;
  14. }
  15. - (id)initWithTitle:(NSString *)title image:(UIImage*)image andblock:(shareBtnBlock)block{
  16. if (self = [super init]) {
  17. self.block = block;
  18. self.title = title;
  19. self.image = image;
  20. }
  21. return self;
  22. }
  23. @end
  24. @interface ZCShareSheetView ()
  25. {
  26. UIView *_alertView;
  27. CGFloat _AllHeight;
  28. NSMutableArray *_marr;
  29. NSInteger _currentSelectIndex;
  30. NSMutableArray* _btnArray;
  31. }
  32. @end
  33. @implementation ZCShareSheetView
  34. - (UIWindow*)alertWindow{
  35. if (!_alertWindow) {
  36. _alertWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
  37. _alertWindow.windowLevel = UIWindowLevelStatusBar+1;
  38. }
  39. return _alertWindow;
  40. }
  41. - (id)initWithTitle:(NSString *)title andShowCancelButton:(BOOL)show andAction:(NSArray<ZCShareActionBtn *> *)arr column:(NSInteger)column{
  42. if (self = [super init]) {
  43. self.frame = [UIScreen mainScreen].bounds;
  44. _btnArray = [NSMutableArray array];
  45. _marr = [NSMutableArray arrayWithArray:arr];
  46. UIControl *control = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
  47. [control addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
  48. control.backgroundColor = [HexColorFromRGB(0x101010) colorWithAlphaComponent:0.5];
  49. // control.alpha = 0.5;
  50. [self addSubview:control];
  51. _alertView = [[UIView alloc]init];
  52. _alertView.backgroundColor = [UIColor whiteColor];
  53. [self addSubview:_alertView];
  54. _currentSelectIndex = index;
  55. CGFloat btnWidth = (KScreenWidth-(column+1)*20.0)/column;
  56. // NSInteger row = arr.count/column+1;//行数
  57. NSInteger row = (NSInteger)ceilf(arr.count/(CGFloat)column);
  58. if (title) {
  59. if (show) {
  60. _AllHeight = ZCShareSheetBtnHeight+ZCShareSheetBtnHeight+row*(btnWidth+20)+(row+1)*20+SafeHeight;
  61. }else{
  62. _AllHeight = ZCShareSheetBtnHeight+row*(btnWidth+20)+(row+1)*20+SafeHeight;
  63. }
  64. _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight);
  65. UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, ZCShareSheetBtnHeight)];
  66. titleLabel.text = title;
  67. titleLabel.textAlignment = NSTextAlignmentCenter;
  68. titleLabel.font = LCBFont17;
  69. titleLabel.textColor = LZ273145Color;
  70. [_alertView addSubview:titleLabel];
  71. UIView* btnBkgView = [[UIView alloc]initWithFrame:CGRectMake(0, ZCShareSheetBtnHeight, KScreenWidth, row*(btnWidth+20)+(row+1)*20)];
  72. // btnBkgView.backgroundColor = [UIColor whiteColor];
  73. [_alertView addSubview:btnBkgView];
  74. for (int i = 0; i < arr.count; i++) {
  75. ZCShareActionBtn *action = arr[i];
  76. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(20+(i%column)*(btnWidth+20), 20+(i/column)*(20+20+btnWidth), btnWidth, btnWidth+20)];
  77. // [button setBackgroundImage:action.image forState:(UIControlStateNormal)];
  78. button.titleLabel.font = LCFont14;
  79. // button.titleEdgeInsets = UIEdgeInsetsMake(btnWidth, 0, 0, 0);
  80. button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 20, 0);
  81. button.titleEdgeInsets = UIEdgeInsetsMake(btnWidth, -btnWidth, 0, 0);
  82. [button setImage:action.image forState:(UIControlStateNormal)];
  83. [button setTitle:action.title forState:UIControlStateNormal];
  84. [button setTitleColor:LZA3AABEColor forState:UIControlStateNormal];
  85. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  86. button.tag=i;
  87. [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside];
  88. [btnBkgView addSubview:button];
  89. [_btnArray addObject:button];
  90. }
  91. if (show) {
  92. UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
  93. cancel.frame = CGRectMake(0, ZCShareSheetBtnHeight+0+row*(btnWidth+20)+(row+1)*20, KScreenWidth, ZCShareSheetBtnHeight);
  94. [cancel setTitle:@"取消" forState:UIControlStateNormal];
  95. [cancel setTitleColor:LZA3AABEColor forState:UIControlStateNormal];
  96. // cancel.backgroundColor = [UIColor whiteColor];
  97. [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
  98. [_alertView addSubview:cancel];
  99. [cancel addLineWithColor:LZF5F4F7Color lineRect:CGRectMake(16.0f, 0.0f, cancel.mj_w - 32.0f, 0.5f)];
  100. }
  101. }else{
  102. if (show) {
  103. _AllHeight = ZCShareSheetBtnHeight+10+row*(btnWidth+20)+(row+1)*20+SafeHeight;
  104. }else{
  105. _AllHeight = row*(btnWidth+20)+(row+1)*20+SafeHeight;
  106. }
  107. _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight);
  108. UIView* btnBkgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, row*(btnWidth+20)+(row+1)*20)];
  109. btnBkgView.backgroundColor = [UIColor whiteColor];
  110. [_alertView addSubview:btnBkgView];
  111. for (int i = 0; i < arr.count; i++) {
  112. ZCShareActionBtn *action = arr[i];
  113. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(20+(i%column)*(btnWidth+20), 20+(i/column)*(20+20+btnWidth), btnWidth, btnWidth+20)];
  114. // [button setBackgroundImage:action.image forState:(UIControlStateNormal)];
  115. button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 20, 0);
  116. button.titleEdgeInsets = UIEdgeInsetsMake(btnWidth, -btnWidth, 0, 0);
  117. button.titleLabel.font = [UIFont systemFontOfSize:15];
  118. [button setImage:action.image forState:(UIControlStateNormal)];
  119. [button setTitle:action.title forState:UIControlStateNormal];
  120. [button setTitleColor:LCTextNormal forState:UIControlStateNormal];
  121. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  122. button.tag=i;
  123. [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside];
  124. [btnBkgView addSubview:button];
  125. [_btnArray addObject:button];
  126. }
  127. if (show) {
  128. UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
  129. cancel.frame = CGRectMake(0, 0+row*(btnWidth+20)+(row+1)*20, KScreenWidth, ZCShareSheetBtnHeight);
  130. [cancel setTitle:@"取消" forState:UIControlStateNormal];
  131. [cancel setTitleColor:HexColorFromRGB(0x9F9DA5) forState:UIControlStateNormal];
  132. // cancel.backgroundColor = [UIColor whiteColor];
  133. [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
  134. [_alertView addSubview:cancel];
  135. [cancel addLineWithColor:[HexColorFromRGB(0x4F4B5B) colorWithAlphaComponent:0.3f] lineRect:CGRectMake(16.0f, 0.0f, cancel.mj_w - 32.0f, 0.5f)];
  136. }
  137. }
  138. [LCTools clipCorner:UIRectCornerTopLeft|UIRectCornerTopRight View:_alertView size:CGSizeMake(20, 20)];
  139. }
  140. return self;
  141. }
  142. - (void)youpaiftapClick:(UIButton *)button{
  143. ZCShareActionBtn *action = _marr[button.tag];
  144. [self hide];
  145. //加个延时操作防止先回调再hide,影响用户体验
  146. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  147. if (action.block) {
  148. action.block(action.title);
  149. }
  150. });
  151. }
  152. - (void)show{
  153. [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
  154. _alertWindow.hidden = NO;
  155. [_alertWindow addSubview:self];
  156. [UIView animateWithDuration:0.3 animations:^{
  157. _alertView.frame = CGRectMake(0, KScreenHeight-_AllHeight, KScreenWidth, _AllHeight);
  158. } completion:^(BOOL finished) {
  159. }];
  160. }
  161. - (void)hide{
  162. [UIView animateWithDuration:0.3 animations:^{
  163. _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight);
  164. } completion:^(BOOL finished) {
  165. [self removeFromSuperview];
  166. _alertWindow.hidden = YES;
  167. _alertWindow = nil;
  168. }];
  169. }
  170. @end