LCActionSheet.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // Created by 刘超 on 15/4/26.
  3. // Copyright (c) 2015年 Leo. All rights reserved.
  4. //
  5. // Email: leoios@sina.com
  6. // GitHub: http://github.com/LeoiOS
  7. // 如有问题或建议请给我发Email, 或在该项目的GitHub主页lssues我, 谢谢:)
  8. //
  9. #import "LCActionSheet.h"
  10. // 按钮高度
  11. #define BUTTON_H 49.0f
  12. // 屏幕尺寸
  13. #define SCREEN_SIZE [UIScreen mainScreen].bounds.size
  14. // 颜色
  15. #define LCColor(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1.0f]
  16. @interface LCActionSheet () {
  17. /** 所有按钮 */
  18. NSArray *_buttonTitles;
  19. /** 暗黑色的view */
  20. UIView *_darkView;
  21. /** 所有按钮的底部view */
  22. UIView *_bottomView;
  23. /** 代理 */
  24. id<LCActionSheetDelegate> _delegate;
  25. }
  26. @property (nonatomic, strong) UIWindow *backWindow;
  27. @end
  28. @implementation LCActionSheet
  29. + (instancetype)sheetWithTitle:(NSString *)title buttonTitles:(NSArray *)titles redButtonIndex:(NSInteger)buttonIndex delegate:(id<LCActionSheetDelegate>)delegate {
  30. return [[self alloc] initWithTitle:title buttonTitles:titles redButtonIndex:buttonIndex delegate:delegate];
  31. }
  32. - (instancetype)initWithTitle:(NSString *)title
  33. buttonTitles:(NSArray *)titles
  34. redButtonIndex:(NSInteger)buttonIndex
  35. delegate:(id<LCActionSheetDelegate>)delegate {
  36. if (self = [super init]) {
  37. _delegate = delegate;
  38. // 暗黑色的view
  39. UIView *darkView = [[UIView alloc] init];
  40. [darkView setAlpha:0];
  41. [darkView setUserInteractionEnabled:NO];
  42. [darkView setFrame:(CGRect){0, 0, SCREEN_SIZE}];
  43. [darkView setBackgroundColor:LCColor(46, 49, 50)];
  44. [self addSubview:darkView];
  45. _darkView = darkView;
  46. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
  47. [darkView addGestureRecognizer:tap];
  48. // 所有按钮的底部view
  49. UIView *bottomView = [[UIView alloc] init];
  50. [bottomView setBackgroundColor:LCColor(233, 233, 238)];
  51. [self addSubview:bottomView];
  52. _bottomView = bottomView;
  53. if (title) {
  54. _title = title;
  55. // 标题
  56. UILabel *label = [[UILabel alloc] init];
  57. [label setText:title];
  58. [label setTextColor:LCColor(111, 111, 111)];
  59. [label setTextAlignment:NSTextAlignmentCenter];
  60. [label setFont:[UIFont systemFontOfSize:13.0f]];
  61. [label setBackgroundColor:[UIColor whiteColor]];
  62. [label setFrame:CGRectMake(0, 0, SCREEN_SIZE.width, BUTTON_H)];
  63. [label setNumberOfLines:0];
  64. // label.lineBreakMode = UILineBreakModeWordWrap;
  65. [bottomView addSubview:label];
  66. }
  67. if (titles.count) {
  68. _buttonTitles = titles;
  69. for (int i = 0; i < titles.count; i++) {
  70. // 所有按钮
  71. UIButton *btn = [[UIButton alloc] init];
  72. [btn setTag:i];
  73. [btn setBackgroundColor:[UIColor whiteColor]];
  74. [btn setTitle:titles[i] forState:UIControlStateNormal];
  75. [[btn titleLabel] setFont:[UIFont systemFontOfSize:16.0f]];
  76. UIColor *titleColor = nil;
  77. if (i == buttonIndex) {
  78. titleColor = LCColor(255, 10, 10);
  79. } else {
  80. titleColor = [UIColor blackColor] ;
  81. }
  82. [btn setTitleColor:titleColor forState:UIControlStateNormal];
  83. [btn setBackgroundImage:[UIImage imageNamed:@"bgImage_HL"] forState:UIControlStateHighlighted];
  84. [btn addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
  85. CGFloat btnY = BUTTON_H * (i + (title ? 1 : 0));
  86. [btn setFrame:CGRectMake(0, btnY, SCREEN_SIZE.width, BUTTON_H)];
  87. [bottomView addSubview:btn];
  88. }
  89. for (int i = 0; i < titles.count; i++) {
  90. // 所有线条
  91. UIImageView *line = [[UIImageView alloc] init];
  92. [line setImage:[UIImage imageNamed:@"cellLine"]];
  93. [line setContentMode:UIViewContentModeCenter];
  94. CGFloat lineY = (i + (title ? 1 : 0)) * BUTTON_H;
  95. [line setFrame:CGRectMake(0, lineY, SCREEN_SIZE.width, 1.0f)];
  96. [bottomView addSubview:line];
  97. }
  98. }
  99. // 取消按钮
  100. UIButton *cancelBtn = [[UIButton alloc] init];
  101. [cancelBtn setTag:titles.count];
  102. [cancelBtn setBackgroundColor:[UIColor whiteColor]];
  103. [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
  104. [[cancelBtn titleLabel] setFont:[UIFont systemFontOfSize:16.0f]];
  105. [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  106. [cancelBtn setBackgroundImage:[UIImage imageNamed:@"bgImage_HL"] forState:UIControlStateHighlighted];
  107. [cancelBtn addTarget:self action:@selector(didClickCancelBtn:) forControlEvents:UIControlEventTouchUpInside];
  108. CGFloat btnY = BUTTON_H * (titles.count + (title ? 1 : 0)) + 5.0f;
  109. [cancelBtn setFrame:CGRectMake(0, btnY, SCREEN_SIZE.width, BUTTON_H+20)];
  110. [bottomView addSubview:cancelBtn];
  111. CGFloat bottomH = (title ? BUTTON_H : 0) + BUTTON_H * titles.count + BUTTON_H + 5.0f;
  112. [bottomView setFrame:CGRectMake(0, SCREEN_SIZE.height, SCREEN_SIZE.width, bottomH+20)];
  113. [self setFrame:(CGRect){0, 0, SCREEN_SIZE}];
  114. [self.backWindow addSubview:self];
  115. }
  116. return self;
  117. }
  118. - (UIWindow *)backWindow {
  119. if (_backWindow == nil) {
  120. _backWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  121. _backWindow.windowLevel = UIWindowLevelStatusBar;
  122. _backWindow.backgroundColor = [UIColor clearColor];
  123. _backWindow.hidden = NO;
  124. }
  125. return _backWindow;
  126. }
  127. - (void)didClickBtn:(UIButton *)btn {
  128. [self dismiss:nil];
  129. if ([_delegate respondsToSelector:@selector(actionSheet:didClickedButton:)]) {
  130. [_delegate actionSheet:self didClickedButton:btn];
  131. }
  132. }
  133. - (void)dismiss:(UITapGestureRecognizer *)tap {
  134. if ([_delegate respondsToSelector:@selector(actionSheet:didClickedBlankPlace:)]) {
  135. [_delegate actionSheet:self didClickedBlankPlace:0];
  136. }
  137. RDS_WEAKSELF(weakSelf)
  138. [UIView animateWithDuration:0.3f
  139. delay:0
  140. options:UIViewAnimationOptionCurveEaseOut
  141. animations:^{
  142. __strong typeof(weakSelf)theSelf = weakSelf;
  143. [theSelf->_darkView setAlpha:0];
  144. [theSelf->_darkView setUserInteractionEnabled:NO];
  145. CGRect frame = theSelf->_bottomView.frame;
  146. frame.origin.y += frame.size.height;
  147. [theSelf->_bottomView setFrame:frame];
  148. }
  149. completion:^(BOOL finished) {
  150. __strong typeof(weakSelf)theSelf = weakSelf;
  151. theSelf->_backWindow.hidden = YES;
  152. [theSelf removeFromSuperview];
  153. }];
  154. }
  155. - (void)didClickCancelBtn:(UIButton *)btn {
  156. RDS_WEAKSELF(weakSelf)
  157. [UIView animateWithDuration:0.3f
  158. delay:0
  159. options:UIViewAnimationOptionCurveEaseOut
  160. animations:^{
  161. __strong typeof(weakSelf)theSelf = weakSelf;
  162. [theSelf->_darkView setAlpha:0];
  163. [theSelf->_darkView setUserInteractionEnabled:NO];
  164. CGRect frame = theSelf->_bottomView.frame;
  165. frame.origin.y += frame.size.height;
  166. [theSelf->_bottomView setFrame:frame];
  167. }
  168. completion:^(BOOL finished) {
  169. __strong typeof(weakSelf)theSelf = weakSelf;
  170. theSelf->_backWindow.hidden = YES;
  171. [theSelf removeFromSuperview];
  172. if ([theSelf->_delegate respondsToSelector:@selector(actionSheet:didClickedButton:)]) {
  173. [theSelf->_delegate actionSheet:theSelf didClickedButton:btn];
  174. }
  175. }];
  176. }
  177. - (void)show {
  178. _backWindow.hidden = NO;
  179. RDS_WEAKSELF(weakSelf)
  180. [UIView animateWithDuration:0.3f
  181. delay:0
  182. options:UIViewAnimationOptionCurveEaseOut
  183. animations:^{
  184. __strong typeof(weakSelf)theSelf = weakSelf;
  185. [theSelf->_darkView setAlpha:0.4f];
  186. [theSelf->_darkView setUserInteractionEnabled:YES];
  187. CGRect frame = theSelf->_bottomView.frame;
  188. frame.origin.y -= frame.size.height;
  189. [theSelf->_bottomView setFrame:frame];
  190. }
  191. completion:nil];
  192. }
  193. - (void)dealloc {
  194. DDLog(@"dealloc");
  195. }
  196. @end