RKActionSheet.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // RKActionSheet.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2020/7/2.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "RKActionSheet.h"
  9. #define sheetHeight 50
  10. @interface RKSheetBtn : UIButton
  11. @property(nonatomic,assign)RKSheetType sheetType;
  12. @property(nonatomic,copy)RKSheetBlock sheetBtnBlock;
  13. @end
  14. @implementation RKSheetBtn
  15. @end
  16. @interface RKActionSheet()<UIGestureRecognizerDelegate>
  17. {
  18. BOOL _ishp;
  19. }
  20. @property(nonatomic,strong)MASViewAttribute *nextMaViewTop;
  21. @property(nonatomic,strong)NSString *titleStr; //标题
  22. @property(nonatomic,strong)UILabel *titleL; //标题 label
  23. @property(nonatomic,strong)UIView *bgView;
  24. @property(nonatomic,strong)UIView *exceptCancelView; //除了except 取消按钮(RKSheet_Cancle)外的所有空间
  25. @property(nonatomic,strong)RKSheetBtn *cancelBtn; //取消按钮
  26. @property(nonatomic,strong)NSMutableArray *exceptCancelMutArray; //除了取消按钮外数组
  27. @end
  28. @implementation RKActionSheet
  29. - (instancetype)initWithTitle:(NSString *)titleStr{
  30. self = [super init];
  31. if (self) {
  32. _titleStr = titleStr;
  33. [self createUI];
  34. }
  35. return self;
  36. }
  37. -(void)createUI {
  38. self.frame = CGRectMake(0, 0, _window_width, _window_height);
  39. self.backgroundColor = RGB_COLOR(@"#000000", 0.3);
  40. self.exceptCancelMutArray = [NSMutableArray array];
  41. [self addSubview:self.bgView];
  42. [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.bottom.equalTo(self.mas_bottom).offset(-ShowDiff-10);
  44. make.width.equalTo(self.mas_width).multipliedBy(0.9);
  45. make.centerX.equalTo(self);
  46. }];
  47. [_bgView addSubview:self.exceptCancelView];
  48. _nextMaViewTop = _exceptCancelView.mas_top;
  49. // [[UIApplication sharedApplication].delegate.window addSubview:self];
  50. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissView)];
  51. tap.delegate = self;
  52. [self addGestureRecognizer:tap];
  53. if (![PublicObj checkNull:_titleStr]) {
  54. [_exceptCancelView addSubview:self.titleL];
  55. [_titleL mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.centerX.equalTo(_exceptCancelView);
  57. make.top.equalTo(_exceptCancelView.mas_top).offset(10);
  58. }];
  59. UILabel *titleLineL = [[UILabel alloc]init];
  60. titleLineL.backgroundColor = RGB_COLOR(@"#969696", 0.8);
  61. [_exceptCancelView addSubview:titleLineL];
  62. [titleLineL mas_makeConstraints:^(MASConstraintMaker *make) {
  63. make.width.centerX.equalTo(_exceptCancelView);
  64. make.height.mas_equalTo(0.5);
  65. make.top.equalTo(_titleL.mas_bottom).offset(18);
  66. }];
  67. _nextMaViewTop = titleLineL.mas_bottom;
  68. }
  69. }
  70. - (UIView *)bgView{
  71. if (!_bgView) {
  72. _bgView = [[UIView alloc]init];
  73. _bgView.backgroundColor = UIColor.clearColor;
  74. }
  75. return _bgView;
  76. }
  77. - (UIView *)exceptCancelView{
  78. if (!_exceptCancelView) {
  79. _exceptCancelView = [[UIView alloc]init];
  80. _exceptCancelView.backgroundColor = [UIColor whiteColor];
  81. _exceptCancelView.layer.cornerRadius = 10;
  82. _exceptCancelView.layer.masksToBounds = YES;
  83. }
  84. return _exceptCancelView;
  85. }
  86. - (UILabel *)titleL {
  87. if (!_titleL) {
  88. _titleL = [[UILabel alloc]init];
  89. _titleL.text = _titleStr;
  90. _titleL.font = SYS_Font(13);
  91. _titleL.textColor = RGB_COLOR(@"#969696", 1);
  92. }
  93. return _titleL;
  94. }
  95. -(void)addLineOfTop:(RKSheetBtn *)btn {
  96. UILabel *lineL = [[UILabel alloc]init];
  97. lineL.backgroundColor = RGB_COLOR(@"#969696", 0.8);
  98. [_exceptCancelView addSubview:lineL];
  99. [lineL mas_makeConstraints:^(MASConstraintMaker *make) {
  100. make.width.centerX.equalTo(_exceptCancelView);
  101. make.height.mas_equalTo(0.5);
  102. make.bottom.equalTo(btn.mas_top);
  103. }];
  104. }
  105. -(void)dismissView {
  106. [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  107. [self removeFromSuperview];
  108. }
  109. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;{
  110. if ([touch.view isDescendantOfView:self.bgView]) {
  111. return NO;
  112. }
  113. return YES;
  114. }
  115. -(void)addActionWithType:(RKSheetType)sheetType andTitle:(NSString *)titile complete:(RKSheetBlock)complete {
  116. RKSheetBtn *btn = [RKSheetBtn buttonWithType:UIButtonTypeCustom];
  117. btn.titleLabel.font = SYS_Font(15);
  118. [btn setTitle:titile forState:0];
  119. btn.sheetBtnBlock = complete;
  120. btn.sheetType = sheetType;
  121. [btn addTarget:self action:@selector(clickActionBtn:) forControlEvents:UIControlEventTouchUpInside];
  122. //这里根据 RKSheetType 来显示 btn 标题颜色 可自行扩展结构体
  123. switch (sheetType) {
  124. case RKSheet_Cancle:
  125. [btn setTitleColor:RGB_COLOR(@"#969696", 1) forState:0];
  126. break;
  127. case RKSheet_Default:
  128. [btn setTitleColor:RGB_COLOR(@"#323232", 1) forState:0];
  129. break;
  130. case RKSheet_FunPink:
  131. [btn setTitleColor:Pink_Cor forState:0];
  132. default:
  133. break;
  134. }
  135. if (btn.sheetType != RKSheet_Cancle) {
  136. [_exceptCancelMutArray addObject:btn];
  137. [_exceptCancelView addSubview:btn];
  138. }else{
  139. btn.layer.cornerRadius = _exceptCancelView.layer.cornerRadius;
  140. btn.layer.masksToBounds = YES;
  141. _cancelBtn = btn;
  142. _cancelBtn.backgroundColor = _exceptCancelView.backgroundColor;
  143. [_bgView addSubview:_cancelBtn];
  144. }
  145. }
  146. -(void)clickActionBtn:(RKSheetBtn*)sender {
  147. if (sender.sheetBtnBlock) {
  148. sender.sheetBtnBlock();
  149. }
  150. [self dismissView];
  151. }
  152. -(void)showLandscapeSheet:(UIView*)view;{
  153. _ishp = YES;
  154. [view.superview addSubview:self];
  155. [self showSheet];
  156. }
  157. -(void)showSheet {
  158. if(!_ishp){
  159. [[UIApplication sharedApplication].delegate.window addSubview:self];
  160. }
  161. //取消按钮
  162. if (_cancelBtn) {
  163. [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  164. make.bottom.equalTo(_bgView.mas_bottom);
  165. make.centerX.width.equalTo(_exceptCancelView);
  166. make.height.mas_equalTo(sheetHeight);
  167. }];
  168. [_exceptCancelView mas_makeConstraints:^(MASConstraintMaker *make) {
  169. make.bottom.equalTo(_cancelBtn.mas_top).offset(-10);
  170. make.width.centerX.equalTo(_bgView);
  171. make.top.equalTo(_bgView.mas_top);
  172. }];
  173. }else{
  174. [_exceptCancelView mas_makeConstraints:^(MASConstraintMaker *make) {
  175. make.bottom.equalTo(_bgView.mas_bottom);
  176. make.width.centerX.equalTo(_bgView);
  177. make.top.equalTo(_bgView.mas_top);
  178. }];
  179. }
  180. //除去取消按钮外其他按钮
  181. MASViewAttribute *bottomeMas = _exceptCancelView.mas_bottom;
  182. for (int i = ((int)_exceptCancelMutArray.count-1); i >= 0; i--) {
  183. RKSheetBtn *btn = _exceptCancelMutArray[i];
  184. [btn mas_makeConstraints:^(MASConstraintMaker *make) {
  185. make.bottom.equalTo(bottomeMas).offset(-0.5);
  186. make.centerX.width.equalTo(_exceptCancelView);
  187. make.height.mas_equalTo(sheetHeight);
  188. if (i == 0) {
  189. make.top.equalTo(_nextMaViewTop).offset(0.5);
  190. }
  191. }];
  192. if (i > 0 ) {
  193. [self addLineOfTop:btn];
  194. }
  195. bottomeMas = btn.mas_top;
  196. }
  197. }
  198. @end