YHActionSheet.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // YHActionSheet.m
  3. // github: https://github.com/samuelandkevin
  4. //
  5. // Created by samuelandkevin on 16/4/28.
  6. // Copyright © 2016年 HKP. All rights reserved.
  7. //
  8. #import "YHActionSheet.h"
  9. const CGFloat YH_Edges = 15.; //左右边距
  10. const CGFloat rowHeight = 44.0f;
  11. const CGFloat footerHeight = 10.0f;
  12. const CGFloat cornerRadius = 10.0f;//圆角大小
  13. /************YHSheetItem************/
  14. @interface YHSheetItem : UITableViewCell
  15. @property (nonatomic,assign) NSInteger otherTitlesCount;
  16. @end
  17. @implementation YHSheetItem
  18. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  19. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  20. self.selectionStyle = UITableViewCellSelectionStyleNone;
  21. self.backgroundColor = [UIColor clearColor];
  22. UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(YH_Edges, 0, SCREEN_WIDTH-YH_Edges*2, rowHeight)];
  23. labelTitle.tag = 101;
  24. labelTitle.backgroundColor = [UIColor whiteColor];
  25. labelTitle.textColor = RGBCOLOR(0, 106, 251);
  26. labelTitle.textAlignment = NSTextAlignmentCenter;
  27. [self.contentView addSubview:labelTitle];
  28. }
  29. return self;
  30. }
  31. /**
  32. * 为Label设置圆角,奇数行:左上角.右上角为圆角;偶数行:左下角.右下角为圆角
  33. *
  34. * @param labelTitle 标题Label
  35. * @param row 行号
  36. * @param allCorner 是否四个角都为圆角
  37. */
  38. - (void)setRectCornerWithLabelTitle:(UILabel *)labelTitle row:(NSInteger)row allCorner:(BOOL)allCorner{
  39. UIBezierPath * bezierPath = nil;
  40. if(allCorner){
  41. bezierPath = [UIBezierPath bezierPathWithRoundedRect:labelTitle.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight|UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
  42. }else{
  43. if(_otherTitlesCount == 1){
  44. bezierPath = [UIBezierPath bezierPathWithRoundedRect:labelTitle.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
  45. }
  46. else{
  47. if (row == 0) {
  48. bezierPath = [UIBezierPath bezierPathWithRoundedRect:labelTitle.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
  49. }
  50. else if(row == _otherTitlesCount-1){
  51. bezierPath = [UIBezierPath bezierPathWithRoundedRect:labelTitle.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
  52. }
  53. else{
  54. bezierPath = [UIBezierPath bezierPathWithRect:labelTitle.bounds ];
  55. }
  56. }
  57. }
  58. CAShapeLayer *maskLayer = [CAShapeLayer layer];
  59. maskLayer.frame = labelTitle.bounds;
  60. maskLayer.path = bezierPath.CGPath;
  61. labelTitle.layer.mask = maskLayer;
  62. }
  63. - (void)layoutSubviews {
  64. [super layoutSubviews];
  65. UIView *view = [self valueForKey:@"_separatorView"];
  66. if (view) {
  67. CGRect frame = view.frame;
  68. frame.origin.x = YH_Edges;
  69. frame.size.width = CGRectGetWidth(self.bounds)- 2*YH_Edges;
  70. view.frame = frame;
  71. }
  72. }
  73. @end
  74. /************YHActionSheet************/
  75. @interface YHActionSheet ()<UITableViewDataSource,UITableViewDelegate>
  76. @property (strong,nonatomic) UIView *backgroundView;//背景图
  77. @property (strong,nonatomic) UITableView *tableview;
  78. @property (nullable, nonatomic, copy) YHSheetCompletionHanlde completionHanlde;
  79. @end
  80. @implementation YHActionSheet
  81. /*
  82. // Only override drawRect: if you perform custom drawing.
  83. // An empty implementation adversely affects performance during animation.
  84. - (void)drawRect:(CGRect)rect {
  85. // Drawing code
  86. }
  87. */
  88. - (instancetype )initWithCancelTitle:(NSString *)cancelTitle otherTitles:(NSArray *)otherTitles{
  89. if (self = [super init]) {
  90. self.cancelTitle = cancelTitle;
  91. self.otherTitles = otherTitles;
  92. [self initSubViews];
  93. }
  94. return self;
  95. }
  96. - (instancetype)initWithFrame:(CGRect)frame{
  97. if(self = [super initWithFrame:frame]){
  98. self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  99. }
  100. return self;
  101. }
  102. #pragma mark - Touch
  103. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
  104. UITouch *touch = (UITouch *)touches.anyObject;
  105. if (touch.view == self.backgroundView) {
  106. if (_completionHanlde) {
  107. self.completionHanlde(NSNotFound, YES);
  108. }
  109. [self dismissViewAnimation];
  110. }
  111. }
  112. #pragma mark - initUI
  113. - (void)initSubViews{
  114. //1.tableview
  115. //tableview高度 = (其他标题+取消标题)*行高 + footer高度*2
  116. CGFloat tableviewH = rowHeight*([self.otherTitles count]+1) + footerHeight*2;
  117. self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT + tableviewH, SCREEN_WIDTH, tableviewH) style:UITableViewStylePlain];
  118. self.tableview.backgroundColor = [UIColor clearColor];
  119. self.tableview.scrollEnabled = NO;
  120. self.tableview.showsVerticalScrollIndicator = NO;
  121. self.tableview.dataSource = self;
  122. self.tableview.delegate = self;
  123. //2.背景view
  124. self.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
  125. self.backgroundView.backgroundColor = [RGBCOLOR(195, 196, 197) colorWithAlphaComponent:0.5];
  126. }
  127. #pragma mark - UITableViewDelegate
  128. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  129. NSInteger index = indexPath.row;
  130. if (_completionHanlde) {
  131. self.completionHanlde(index, indexPath.section == 1 ? YES : NO);
  132. }
  133. [self dismissViewAnimation];
  134. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  135. }
  136. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  137. return 2;
  138. }
  139. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  140. UIView *footerView =[[UIView alloc] init];
  141. footerView.backgroundColor = [UIColor clearColor];
  142. return footerView;
  143. }
  144. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  145. return footerHeight;
  146. }
  147. #pragma mark - UITableViewDataSource
  148. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  149. if (section == 0) {
  150. return [self.otherTitles count];
  151. }else{
  152. return 1;
  153. }
  154. }
  155. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  156. YHSheetItem *cell =[tableView dequeueReusableCellWithIdentifier:@"cellId"];
  157. if (!cell) {
  158. cell = [[YHSheetItem alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
  159. }
  160. cell.otherTitlesCount = [self.otherTitles count];
  161. UILabel *label = [cell.contentView viewWithTag:101];
  162. if (indexPath.section == 0) {
  163. label.text = self.otherTitles[indexPath.row];
  164. [cell setRectCornerWithLabelTitle:label row:indexPath.row allCorner:NO];
  165. }
  166. else{
  167. label.text = self.cancelTitle;
  168. [cell setRectCornerWithLabelTitle:label row:indexPath.row allCorner:YES];
  169. }
  170. return cell;
  171. }
  172. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  173. return rowHeight;
  174. }
  175. #pragma mark - Private
  176. - (UIWindow *)statusBarWindow {
  177. return [UIApplication sharedApplication].keyWindow;
  178. // valueForKey:@"_statusBarWindow"];
  179. }
  180. - (UIInterfaceOrientation)appInterface {
  181. return [UIApplication sharedApplication].statusBarOrientation;
  182. }
  183. - (void)showViewAnimation {
  184. __weak typeof(self) wSelf = self;
  185. self.backgroundView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  186. [UIView animateWithDuration:0.3 animations:^{
  187. CGRect frame = _tableview.frame;
  188. frame.origin.y = CGRectGetHeight(wSelf.bounds) - frame.size.height;
  189. wSelf.tableview.frame = frame;
  190. }];
  191. }
  192. - (void)dismissViewAnimation {
  193. __weak typeof(self) wSelf = self;
  194. self.backgroundView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
  195. [UIView animateWithDuration:0.3 animations:^{
  196. CGRect frame = _tableview.frame;
  197. frame.origin.y = CGRectGetHeight(wSelf.bounds);
  198. wSelf.tableview.frame = frame;
  199. } completion:^(BOOL finished) {
  200. if (finished) {
  201. [wSelf removeFromSuperview];
  202. }
  203. }];
  204. }
  205. #pragma mark - Public
  206. - (void)show{
  207. UIWindow *statusBarWindow = [self statusBarWindow];
  208. [statusBarWindow addSubview:self];
  209. [self addSubview:self.backgroundView];
  210. [self addSubview:self.tableview];
  211. [self showViewAnimation];
  212. }
  213. - (void)dismissForCompletionHandle:(nullable YHSheetCompletionHanlde)handle{
  214. self.completionHanlde = handle;
  215. }
  216. @end