ActionSheetView.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // ActionSheetView.m
  3. // MingMen
  4. //
  5. // Created by 罗云飞 on 2017/3/9.
  6. // Copyright © 2017年 罗云飞. All rights reserved.
  7. //
  8. #import "ActionSheetView.h"
  9. #define ACTIONSHEET_BACKGROUNDCOLOR [UIColor colorWithRed:1.00f green:1.00f blue:1.00f alpha:1]
  10. #define WINDOW_COLOR [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]
  11. #define ANIMATE_DURATION 0.25f
  12. #define ActionSheetW [[UIScreen mainScreen] bounds].size.width
  13. #define ActionSheetH [[UIScreen mainScreen] bounds].size.height
  14. @interface ActionSheetView ()
  15. @property (nonatomic,assign) CGFloat LXActionSheetHeight;
  16. @property (nonatomic,strong) NSArray *shareBtnTitleArray;
  17. @property (nonatomic,strong) NSArray *shareBtnImgArray;
  18. @property (nonatomic,strong) UIView *backGroundView;
  19. @property (nonatomic,strong) UIView *topsheetView;
  20. @property (nonatomic,strong) UIButton *cancelBtn;
  21. //头部提示文字Label
  22. @property (nonatomic,strong) UILabel *proL;
  23. @property (nonatomic,copy) NSString *protext;
  24. @property (nonatomic,assign) ShowType showtype;
  25. @end
  26. @implementation ActionSheetView
  27. - (id)initWithShareHeadOprationWith:(NSArray *)titleArray andImageArry:(NSArray *)imageArr andProTitle:(NSString *)protitle and:(ShowType)type
  28. {
  29. self = [super init];
  30. if (self) {
  31. self.shareBtnImgArray = imageArr;
  32. self.shareBtnTitleArray = titleArray;
  33. _protext = protitle;
  34. _showtype = type;
  35. self.frame = CGRectMake(0, 0, ActionSheetW, ActionSheetH);
  36. self.backgroundColor = WINDOW_COLOR;
  37. self.userInteractionEnabled = YES;
  38. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
  39. [self addGestureRecognizer:tapGesture];
  40. if (type == ShowTypeIsShareStyle) {
  41. [self loadUiConfig];
  42. }
  43. else
  44. {
  45. [self loadActionSheetUi];
  46. }
  47. }
  48. return self;
  49. }
  50. - (void)setCancelBtnColor:(UIColor *)cancelBtnColor
  51. {
  52. [_cancelBtn setTitleColor:cancelBtnColor forState:UIControlStateNormal];
  53. }
  54. //- (void)setProStr:(NSString *)proStr
  55. //{
  56. // _proL.text = proStr;
  57. //}
  58. - (void)setOtherBtnColor:(UIColor *)otherBtnColor
  59. {
  60. for (id res in _backGroundView.subviews) {
  61. if ([res isKindOfClass:[UIButton class]]) {
  62. UIButton *button = (UIButton *)res;
  63. if (button.tag>=100) {
  64. [button setTitleColor:otherBtnColor forState:UIControlStateNormal];
  65. }
  66. }
  67. }
  68. }
  69. - (void)setOtherBtnFont:(NSInteger)otherBtnFont
  70. {
  71. for (id res in _backGroundView.subviews) {
  72. if ([res isKindOfClass:[UIButton class]]) {
  73. UIButton *button = (UIButton *)res;
  74. if (button.tag>=100) {
  75. button.titleLabel.font = [UIFont systemFontOfSize:otherBtnFont];
  76. }
  77. }
  78. }
  79. }
  80. -(void)setProFont:(NSInteger)proFont
  81. {
  82. _proL.font = [UIFont systemFontOfSize:proFont];
  83. }
  84. - (void)setCancelBtnFont:(NSInteger)cancelBtnFont
  85. {
  86. _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:cancelBtnFont];
  87. }
  88. - (void)setDuration:(CGFloat)duration
  89. {
  90. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:duration];
  91. }
  92. - (void)loadActionSheetUi
  93. {
  94. [self addSubview:self.backGroundView];
  95. [_backGroundView addSubview:self.cancelBtn];
  96. if (_protext.length) {
  97. [_backGroundView addSubview:self.proL];
  98. }
  99. for (NSInteger i = 0; i<_shareBtnTitleArray.count; i++) {
  100. VerButton *button = [VerButton buttonWithType:UIButtonTypeCustom];
  101. button.frame = CGRectMake(0, CGRectGetHeight(_proL.frame)+50*i, CGRectGetWidth(_backGroundView.frame), 50);
  102. [button setTitle:_shareBtnTitleArray[i] forState:UIControlStateNormal];
  103. [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  104. button.tag = 100+i;
  105. [button addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
  106. [_backGroundView addSubview:button];
  107. }
  108. [UIView animateWithDuration:ANIMATE_DURATION animations:^{
  109. _backGroundView.frame = CGRectMake(0, ActionSheetH-(_shareBtnTitleArray.count*50+50)-7-(_protext.length==0?0:45), ActionSheetW, _shareBtnTitleArray.count*50+50+7+(_protext.length==0?0:45));
  110. }];
  111. }
  112. - (void)loadUiConfig
  113. {
  114. [self addSubview:self.backGroundView];
  115. [_backGroundView addSubview:self.topsheetView];
  116. [_backGroundView addSubview:self.cancelBtn];
  117. _LXActionSheetHeight = CGRectGetHeight(_proL.frame)+7;
  118. for (NSInteger i = 0; i<_shareBtnImgArray.count; i++)
  119. {
  120. ActionButton *button = [ActionButton buttonWithType:UIButtonTypeCustom];
  121. if (_shareBtnImgArray.count%3 == 0) {
  122. button.frame = CGRectMake(_backGroundView.bounds.size.width/3*(i%3), _LXActionSheetHeight+(i/3)*76, _backGroundView.bounds.size.width/3, 70);
  123. }
  124. else
  125. {
  126. button.frame = CGRectMake(_backGroundView.bounds.size.width/4*(i%4), _LXActionSheetHeight+(i/4)*76, _backGroundView.bounds.size.width/4, 70);
  127. }
  128. [button setTitle:_shareBtnTitleArray[i] forState:UIControlStateNormal];
  129. [button setImage:[UIImage imageNamed:_shareBtnImgArray[i]] forState:UIControlStateNormal];
  130. button.tag = 200+i;
  131. [button addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
  132. [self.topsheetView addSubview:button];
  133. }
  134. [UIView animateWithDuration:ANIMATE_DURATION animations:^{
  135. _backGroundView.frame = CGRectMake(7, ActionSheetH-CGRectGetHeight(_backGroundView.frame), ActionSheetW-14, CGRectGetHeight(_backGroundView.frame));
  136. }];
  137. }
  138. - (void)BtnClick:(UIButton *)btn
  139. {
  140. [self tappedCancel];
  141. if (btn.tag<200) {
  142. _btnClick(btn.tag-100);
  143. }
  144. else
  145. {
  146. _btnClick(btn.tag-200);
  147. }
  148. }
  149. - (void)tappedCancel
  150. {
  151. [UIView animateWithDuration:ANIMATE_DURATION animations:^{
  152. [self.backGroundView setFrame:CGRectMake(0, ActionSheetH, ActionSheetW, 0)];
  153. self.alpha = 0;
  154. } completion:^(BOOL finished) {
  155. if (finished) {
  156. [self removeFromSuperview];
  157. }
  158. }];
  159. }
  160. - (void)noTap
  161. {}
  162. #pragma mark -------- getter
  163. - (UIView *)backGroundView
  164. {
  165. if (_backGroundView == nil) {
  166. _backGroundView = [[UIView alloc] init];
  167. if (_showtype == ShowTypeIsShareStyle) {
  168. if (_shareBtnImgArray.count<5) {
  169. _backGroundView.frame = CGRectMake(7, ActionSheetH, ActionSheetW-14, 64+(_protext.length==0?0:45)+76+14);
  170. }else
  171. {
  172. NSInteger index;
  173. if (_shareBtnTitleArray.count%4 ==0) {
  174. index =_shareBtnTitleArray.count/4;
  175. }
  176. else
  177. {
  178. index = _shareBtnTitleArray.count/4 + 1;
  179. }
  180. _backGroundView.frame = CGRectMake(7, ActionSheetH, ActionSheetW-14, 64+(_protext.length==0?0:45)+76*index+14);
  181. }
  182. }
  183. else
  184. {
  185. _backGroundView.frame = CGRectMake(0, ActionSheetH, ActionSheetW, _shareBtnTitleArray.count*50+50+7+(_protext.length==0?0:45));
  186. _backGroundView.backgroundColor = [UIColor colorWithRed:0.89f green:0.89f blue:0.89f alpha:1.00f];
  187. }
  188. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(noTap)];
  189. [_backGroundView addGestureRecognizer:tapGesture];
  190. }
  191. return _backGroundView;
  192. }
  193. - (UIView *)topsheetView
  194. {
  195. if (_topsheetView == nil) {
  196. _topsheetView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_backGroundView.frame), CGRectGetHeight(_backGroundView.frame)-64)];
  197. _topsheetView.backgroundColor = [UIColor whiteColor];
  198. _topsheetView.layer.cornerRadius = 4;
  199. _topsheetView.clipsToBounds = YES;
  200. if (_protext.length) {
  201. [_topsheetView addSubview:self.proL];
  202. }
  203. }
  204. return _topsheetView;
  205. }
  206. - (UILabel *)proL
  207. {
  208. if (_proL == nil) {
  209. _proL = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_backGroundView.frame), 45)];
  210. _proL.text = _protext;
  211. _proL.textColor = [UIColor grayColor];
  212. _proL.backgroundColor = [UIColor whiteColor];
  213. _proL.textAlignment = NSTextAlignmentCenter;
  214. }
  215. return _proL;
  216. }
  217. - (UIButton *)cancelBtn
  218. {
  219. if (_cancelBtn == nil) {
  220. _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  221. if (_showtype == ShowTypeIsShareStyle) {
  222. _cancelBtn.frame = CGRectMake(0, CGRectGetHeight(_backGroundView.frame)-57, CGRectGetWidth(_backGroundView.frame), 50);
  223. _cancelBtn.layer.cornerRadius = 4;
  224. _cancelBtn.clipsToBounds = YES;
  225. }
  226. else
  227. {
  228. _cancelBtn.frame = CGRectMake(0, CGRectGetHeight(_backGroundView.frame)-50, CGRectGetWidth(_backGroundView.frame), 50);
  229. }
  230. [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
  231. _cancelBtn.backgroundColor = [UIColor whiteColor];
  232. [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  233. [_cancelBtn addTarget:self action:@selector(tappedCancel) forControlEvents:UIControlEventTouchUpInside];
  234. }
  235. return _cancelBtn;
  236. }
  237. @end