LCActionSheet.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. //
  2. // LCActionSheet.m
  3. // LCActionSheet
  4. //
  5. // Created by Leo on 2015/4/27.
  6. //
  7. // Copyright (c) 2015-2017 Leo <leodaxia@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in all
  17. // copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. // SOFTWARE.
  26. #import "LCActionSheet.h"
  27. #import "LCActionSheetCell.h"
  28. #import "LCActionSheetViewController.h"
  29. #import "UIImage+LCActionSheet.h"
  30. #import "Masonry.h"
  31. #import "MH_MODIFY_LC.h"
  32. /// 取消按钮顶部的间隙 : 系统默认:5.0f Custom By RQ :10.0f
  33. static CGFloat const LCCancelButtonTopMargin = 10.0f;
  34. @interface LCActionSheet () <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
  35. @property (nonatomic, strong) NSArray<NSString *> *otherButtonTitles;
  36. @property (nonatomic, assign) CGSize titleTextSize;
  37. @property (nonatomic, weak) UIView *bottomView;
  38. @property (nonatomic, weak) UIVisualEffectView *blurEffectView;
  39. @property (nonatomic, weak) UIView *darkView;
  40. @property (nonatomic, weak) UILabel *titleLabel;
  41. @property (nonatomic, weak) UITableView *tableView;
  42. @property (nonatomic, weak) UIView *divisionView;
  43. @property (nonatomic, weak) UIButton *cancelButton;
  44. @property (nonatomic, weak) UIView *whiteBgView;
  45. @property (nonatomic, weak) UIView *lineView;
  46. @property (nullable, nonatomic, strong) UIWindow *window;
  47. @end
  48. @implementation LCActionSheet
  49. - (void)dealloc {
  50. [[NSNotificationCenter defaultCenter] removeObserver:self];
  51. }
  52. + (instancetype)sheetWithTitle:(NSString *)title delegate:(id<LCActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
  53. id eachObject;
  54. va_list argumentList;
  55. NSMutableArray *tempOtherButtonTitles = nil;
  56. if (otherButtonTitles) {
  57. tempOtherButtonTitles = [[NSMutableArray alloc] initWithObjects:otherButtonTitles, nil];
  58. va_start(argumentList, otherButtonTitles);
  59. while ((eachObject = va_arg(argumentList, id))) {
  60. [tempOtherButtonTitles addObject:eachObject];
  61. }
  62. va_end(argumentList);
  63. }
  64. return [[self alloc] initWithTitle:title
  65. delegate:delegate
  66. cancelButtonTitle:cancelButtonTitle
  67. otherButtonTitleArray:tempOtherButtonTitles];
  68. }
  69. + (instancetype)sheetWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle clicked:(LCActionSheetClickedHandler)clickedHandler otherButtonTitles:(NSString *)otherButtonTitles, ... {
  70. id eachObject;
  71. va_list argumentList;
  72. NSMutableArray *tempOtherButtonTitles = nil;
  73. if (otherButtonTitles) {
  74. tempOtherButtonTitles = [[NSMutableArray alloc] initWithObjects:otherButtonTitles, nil];
  75. va_start(argumentList, otherButtonTitles);
  76. while ((eachObject = va_arg(argumentList, id))) {
  77. [tempOtherButtonTitles addObject:eachObject];
  78. }
  79. va_end(argumentList);
  80. }
  81. return [[self alloc] initWithTitle:title
  82. cancelButtonTitle:cancelButtonTitle
  83. clicked:clickedHandler
  84. otherButtonTitleArray:tempOtherButtonTitles];
  85. }
  86. + (instancetype)sheetWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle didDismiss:(LCActionSheetDidDismissHandler)didDismissHandler otherButtonTitles:(NSString *)otherButtonTitles, ... {
  87. id eachObject;
  88. va_list argumentList;
  89. NSMutableArray *tempOtherButtonTitles = nil;
  90. if (otherButtonTitles) {
  91. tempOtherButtonTitles = [[NSMutableArray alloc] initWithObjects:otherButtonTitles, nil];
  92. va_start(argumentList, otherButtonTitles);
  93. while ((eachObject = va_arg(argumentList, id))) {
  94. [tempOtherButtonTitles addObject:eachObject];
  95. }
  96. va_end(argumentList);
  97. }
  98. return [[self alloc] initWithTitle:title
  99. cancelButtonTitle:cancelButtonTitle
  100. didDismiss:didDismissHandler
  101. otherButtonTitleArray:tempOtherButtonTitles];
  102. }
  103. + (instancetype)sheetWithTitle:(NSString *)title delegate:(id<LCActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitleArray:(NSArray<NSString *> *)otherButtonTitleArray {
  104. return [[self alloc] initWithTitle:title
  105. delegate:delegate
  106. cancelButtonTitle:cancelButtonTitle
  107. otherButtonTitleArray:otherButtonTitleArray];
  108. }
  109. + (instancetype)sheetWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle clicked:(LCActionSheetClickedHandler)clickedHandler otherButtonTitleArray:(NSArray<NSString *> *)otherButtonTitleArray {
  110. return [[self alloc] initWithTitle:title
  111. cancelButtonTitle:cancelButtonTitle
  112. clicked:clickedHandler
  113. otherButtonTitleArray:otherButtonTitleArray];
  114. }
  115. + (instancetype)sheetWithTitle:(NSString *)title cancelButtonTitle:(nullable NSString *)cancelButtonTitle didDismiss:(nullable LCActionSheetDidDismissHandler)didDismissHandler otherButtonTitleArray:(nullable NSArray<NSString *> *)otherButtonTitleArray {
  116. return [[self alloc] initWithTitle:title
  117. cancelButtonTitle:cancelButtonTitle
  118. didDismiss:didDismissHandler
  119. otherButtonTitleArray:otherButtonTitleArray];
  120. }
  121. - (instancetype)initWithTitle:(NSString *)title delegate:(id<LCActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
  122. if (self = [super init]) {
  123. [self config:LCActionSheetConfig.config];
  124. id eachObject;
  125. va_list argumentList;
  126. NSMutableArray *tempOtherButtonTitles = nil;
  127. if (otherButtonTitles) {
  128. tempOtherButtonTitles = [[NSMutableArray alloc] initWithObjects:otherButtonTitles, nil];
  129. va_start(argumentList, otherButtonTitles);
  130. while ((eachObject = va_arg(argumentList, id))) {
  131. [tempOtherButtonTitles addObject:eachObject];
  132. }
  133. va_end(argumentList);
  134. }
  135. self.title = title;
  136. self.delegate = delegate;
  137. self.cancelButtonTitle = cancelButtonTitle;
  138. self.otherButtonTitles = tempOtherButtonTitles;
  139. [self setupView];
  140. }
  141. return self;
  142. }
  143. - (instancetype)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle clicked:(LCActionSheetClickedHandler)clickedHandler otherButtonTitles:(NSString *)otherButtonTitles, ... {
  144. if (self = [super init]) {
  145. [self config:LCActionSheetConfig.config];
  146. id eachObject;
  147. va_list argumentList;
  148. NSMutableArray *tempOtherButtonTitles = nil;
  149. if (otherButtonTitles) {
  150. tempOtherButtonTitles = [[NSMutableArray alloc] initWithObjects:otherButtonTitles, nil];
  151. va_start(argumentList, otherButtonTitles);
  152. while ((eachObject = va_arg(argumentList, id))) {
  153. [tempOtherButtonTitles addObject:eachObject];
  154. }
  155. va_end(argumentList);
  156. }
  157. self.title = title;
  158. self.cancelButtonTitle = cancelButtonTitle;
  159. self.clickedHandler = clickedHandler;
  160. self.otherButtonTitles = tempOtherButtonTitles;
  161. [self setupView];
  162. }
  163. return self;
  164. }
  165. - (instancetype)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle didDismiss:(LCActionSheetDidDismissHandler)didDismissHandler otherButtonTitles:(NSString *)otherButtonTitles, ... {
  166. if (self = [super init]) {
  167. [self config:LCActionSheetConfig.config];
  168. id eachObject;
  169. va_list argumentList;
  170. NSMutableArray *tempOtherButtonTitles = nil;
  171. if (otherButtonTitles) {
  172. tempOtherButtonTitles = [[NSMutableArray alloc] initWithObjects:otherButtonTitles, nil];
  173. va_start(argumentList, otherButtonTitles);
  174. while ((eachObject = va_arg(argumentList, id))) {
  175. [tempOtherButtonTitles addObject:eachObject];
  176. }
  177. va_end(argumentList);
  178. }
  179. self.title = title;
  180. self.cancelButtonTitle = cancelButtonTitle;
  181. self.didDismissHandler = didDismissHandler;
  182. self.otherButtonTitles = tempOtherButtonTitles;
  183. [self setupView];
  184. }
  185. return self;
  186. }
  187. - (instancetype)initWithTitle:(NSString *)title delegate:(id<LCActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitleArray:(NSArray<NSString *> *)otherButtonTitleArray {
  188. if (self = [super init]) {
  189. [self config:LCActionSheetConfig.config];
  190. self.title = title;
  191. self.delegate = delegate;
  192. self.cancelButtonTitle = cancelButtonTitle;
  193. self.otherButtonTitles = otherButtonTitleArray;
  194. [self setupView];
  195. }
  196. return self;
  197. }
  198. - (instancetype)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle clicked:(LCActionSheetClickedHandler)clickedHandler otherButtonTitleArray:(NSArray<NSString *> *)otherButtonTitleArray {
  199. if (self = [super init]) {
  200. [self config:LCActionSheetConfig.config];
  201. self.title = title;
  202. self.cancelButtonTitle = cancelButtonTitle;
  203. self.clickedHandler = clickedHandler;
  204. self.otherButtonTitles = otherButtonTitleArray;
  205. [self setupView];
  206. }
  207. return self;
  208. }
  209. - (instancetype)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle didDismiss:(LCActionSheetDidDismissHandler)didDismissHandler otherButtonTitleArray:(NSArray<NSString *> *)otherButtonTitleArray {
  210. if (self = [super init]) {
  211. [self config:LCActionSheetConfig.config];
  212. self.title = title;
  213. self.cancelButtonTitle = cancelButtonTitle;
  214. self.didDismissHandler = didDismissHandler;
  215. self.otherButtonTitles = otherButtonTitleArray;
  216. [self setupView];
  217. }
  218. return self;
  219. }
  220. - (instancetype)config:(LCActionSheetConfig *)config {
  221. _title = config.title;
  222. _cancelButtonTitle = config.cancelButtonTitle;
  223. _destructiveButtonIndexSet = config.destructiveButtonIndexSet;
  224. _destructiveButtonColor = config.destructiveButtonColor;
  225. _titleColor = config.titleColor;
  226. _buttonColor = config.buttonColor;
  227. _titleFont = config.titleFont;
  228. _buttonFont = config.buttonFont;
  229. _buttonHeight = config.buttonHeight;
  230. _scrolling = config.canScrolling;
  231. _visibleButtonCount = config.visibleButtonCount;
  232. _animationDuration = config.animationDuration;
  233. _darkOpacity = config.darkOpacity;
  234. _darkViewNoTaped = config.darkViewNoTaped;
  235. _unBlur = config.unBlur;
  236. _blurEffectStyle = config.blurEffectStyle;
  237. _titleEdgeInsets = config.titleEdgeInsets;
  238. _separatorColor = config.separatorColor;
  239. _autoHideWhenDeviceRotated = config.autoHideWhenDeviceRotated;
  240. return self;
  241. }
  242. - (void)setupView {
  243. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  244. [notificationCenter addObserver:self
  245. selector:@selector(handleDidChangeStatusBarOrientation)
  246. name:UIApplicationDidChangeStatusBarOrientationNotification
  247. object:nil];
  248. UIView *bottomView = [[UIView alloc] init];
  249. [self addSubview:bottomView];
  250. [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  251. make.left.right.equalTo(self);
  252. CGFloat height =
  253. (self.title.length > 0 ? self.titleTextSize.height + 2.0f + (self.titleEdgeInsets.top + self.titleEdgeInsets.bottom) : 0) +
  254. (self.otherButtonTitles.count > 0 ? (self.canScrolling ? MIN(self.visibleButtonCount, self.otherButtonTitles.count) : self.otherButtonTitles.count) * self.buttonHeight : 0) +
  255. (self.cancelButtonTitle.length > 0 ? LCCancelButtonTopMargin + self.buttonHeight : 0);
  256. make.height.equalTo(@(height));
  257. make.bottom.equalTo(self).offset(height);
  258. }];
  259. self.bottomView = bottomView;
  260. UIView *darkView = [[UIView alloc] init];
  261. darkView.alpha = 0;
  262. darkView.userInteractionEnabled = NO;
  263. #if USER_MODIFY_LC_BY_RQ
  264. darkView.backgroundColor = LC_ACTION_SHEET_COLOR(0, 0, 0);
  265. #else
  266. darkView.backgroundColor = LC_ACTION_SHEET_COLOR(46, 49, 50);
  267. #endif
  268. [self addSubview:darkView];
  269. [darkView mas_makeConstraints:^(MASConstraintMaker *make) {
  270. make.left.right.equalTo(self);
  271. make.top.equalTo(self).priorityLow();
  272. make.bottom.equalTo(bottomView.mas_top);
  273. }];
  274. self.darkView = darkView;
  275. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
  276. action:@selector(darkViewClicked)];
  277. [darkView addGestureRecognizer:tap];
  278. UIView *whiteBgView = [[UIView alloc] init];
  279. whiteBgView.backgroundColor = [UIColor whiteColor];
  280. [bottomView addSubview:whiteBgView];
  281. [whiteBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  282. make.edges.equalTo(bottomView);
  283. }];
  284. self.whiteBgView = whiteBgView;
  285. if (!self.unBlur) {
  286. [self blurBottomBgView];
  287. } else {
  288. whiteBgView.hidden = NO;
  289. }
  290. UILabel *titleLabel = [[UILabel alloc] init];
  291. titleLabel.text = self.title;
  292. titleLabel.font = self.titleFont;
  293. titleLabel.numberOfLines = 0;
  294. titleLabel.textColor = self.titleColor;
  295. titleLabel.textAlignment = NSTextAlignmentCenter;
  296. [bottomView addSubview:titleLabel];
  297. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  298. make.top.equalTo(bottomView).offset(self.title.length > 0 ? self.titleEdgeInsets.top : 0);
  299. make.left.equalTo(bottomView).offset(self.titleEdgeInsets.left);
  300. make.right.equalTo(bottomView).offset(-self.titleEdgeInsets.right);
  301. CGFloat height = self.title.length > 0 ? self.titleTextSize.height + 2.0f : 0; // Prevent omit
  302. make.height.equalTo(@(height));
  303. }];
  304. self.titleLabel = titleLabel;
  305. UITableView *tableView = [[UITableView alloc] init];
  306. tableView.backgroundColor = [UIColor clearColor];
  307. tableView.dataSource = self;
  308. tableView.delegate = self;
  309. [bottomView addSubview:tableView];
  310. [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  311. make.left.right.equalTo(bottomView);
  312. make.top.equalTo(titleLabel.mas_bottom).offset(self.title.length > 0 ? self.titleEdgeInsets.bottom : 0);
  313. CGFloat height = self.otherButtonTitles.count * self.buttonHeight;
  314. make.height.equalTo(@(height));
  315. }];
  316. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  317. self.tableView = tableView;
  318. UIView *lineView = [[UIView alloc] init];
  319. lineView.backgroundColor = self.separatorColor;
  320. lineView.contentMode = UIViewContentModeBottom;
  321. lineView.clipsToBounds = YES;
  322. [bottomView addSubview:lineView];
  323. [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
  324. make.left.right.equalTo(bottomView);
  325. make.bottom.equalTo(tableView.mas_top);
  326. make.height.equalTo(@0.5f);
  327. }];
  328. self.lineView = lineView;
  329. self.lineView.hidden = !self.title || self.title.length == 0;
  330. UIView *divisionView = [[UIView alloc] init];
  331. divisionView.alpha = 0.3f;
  332. divisionView.backgroundColor = self.separatorColor;
  333. [bottomView addSubview:divisionView];
  334. [divisionView mas_makeConstraints:^(MASConstraintMaker *make) {
  335. make.left.right.equalTo(bottomView);
  336. make.top.equalTo(tableView.mas_bottom);
  337. CGFloat height = self.cancelButtonTitle.length > 0 ? LCCancelButtonTopMargin : 0;
  338. make.height.equalTo(@(height));
  339. }];
  340. self.divisionView = divisionView;
  341. UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
  342. cancelButton.backgroundColor = [UIColor clearColor];
  343. cancelButton.titleLabel.font = self.buttonFont;
  344. [cancelButton setTitle:self.cancelButtonTitle forState:UIControlStateNormal];
  345. [cancelButton setTitleColor:self.buttonColor forState:UIControlStateNormal];
  346. #if USER_MODIFY_LC_BY_RQ
  347. /// fix 掉通过 LCActionSheetConfig 配置actionsheet的bug
  348. if ([self.destructiveButtonIndexSet containsIndex:0]) {
  349. [cancelButton setTitleColor:self.destructiveButtonColor forState:UIControlStateNormal];
  350. }
  351. #endif
  352. [cancelButton setBackgroundImage:[UIImage lc_imageWithColor:self.separatorColor]
  353. forState:UIControlStateHighlighted];
  354. [cancelButton addTarget:self
  355. action:@selector(cancelButtonClicked)
  356. forControlEvents:UIControlEventTouchUpInside];
  357. [bottomView addSubview:cancelButton];
  358. [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
  359. make.left.right.equalTo(bottomView);
  360. make.bottom.equalTo(bottomView);
  361. CGFloat height = self.cancelButtonTitle.length > 0 ? self.buttonHeight : 0;
  362. make.height.equalTo(@(height));
  363. }];
  364. self.cancelButton = cancelButton;
  365. }
  366. - (void)appendButtonsWithTitles:(NSString *)titles, ... {
  367. id eachObject;
  368. va_list argumentList;
  369. NSMutableArray *tempButtonTitles = nil;
  370. if (titles) {
  371. tempButtonTitles = [[NSMutableArray alloc] initWithObjects:titles, nil];
  372. va_start(argumentList, titles);
  373. while ((eachObject = va_arg(argumentList, id))) {
  374. [tempButtonTitles addObject:eachObject];
  375. }
  376. va_end(argumentList);
  377. }
  378. self.otherButtonTitles = [self.otherButtonTitles arrayByAddingObjectsFromArray:tempButtonTitles];
  379. [self.tableView reloadData];
  380. [self updateBottomView];
  381. [self updateTableView];
  382. }
  383. - (void)appendButtonWithTitle:(NSString *)title atIndex:(NSInteger)index {
  384. #ifdef DEBUG
  385. NSAssert(index != 0, @"Index 0 is cancel button");
  386. NSAssert(index <= self.otherButtonTitles.count + 1, @"Index crossed");
  387. #endif
  388. NSMutableArray<NSString *> *arrayM = [NSMutableArray arrayWithArray:self.otherButtonTitles];
  389. [arrayM insertObject:title atIndex:index - 1];
  390. self.otherButtonTitles = [NSArray arrayWithArray:arrayM];
  391. [self.tableView reloadData];
  392. [self updateBottomView];
  393. [self updateTableView];
  394. }
  395. - (void)appendButtonsWithTitles:(NSArray<NSString *> *)titles atIndexes:(NSIndexSet *)indexes {
  396. #ifdef DEBUG
  397. NSAssert(titles.count == indexes.count, @"Count of titles differs from count of indexs");
  398. #endif
  399. NSMutableIndexSet *indexSetM = [[NSMutableIndexSet alloc] init];
  400. [indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
  401. #ifdef DEBUG
  402. NSAssert(idx != 0, @"Index 0 is cancel button");
  403. NSAssert(idx <= self.otherButtonTitles.count + indexes.count, @"Index crossed");
  404. #endif
  405. [indexSetM addIndex:idx - 1];
  406. }];
  407. NSMutableArray<NSString *> *arrayM = [NSMutableArray arrayWithArray:self.otherButtonTitles];
  408. [arrayM insertObjects:titles atIndexes:indexSetM];
  409. self.otherButtonTitles = [NSArray arrayWithArray:arrayM];
  410. [self.tableView reloadData];
  411. [self updateBottomView];
  412. [self updateTableView];
  413. }
  414. - (void)handleDidChangeStatusBarOrientation {
  415. if (self.autoHideWhenDeviceRotated) {
  416. [self hideWithButtonIndex:self.cancelButtonIndex];
  417. }
  418. }
  419. - (void)blurBottomBgView {
  420. self.whiteBgView.hidden = YES;
  421. if (!self.blurEffectView) {
  422. UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:self.blurEffectStyle];
  423. UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
  424. [self.bottomView addSubview:blurEffectView];
  425. [blurEffectView mas_makeConstraints:^(MASConstraintMaker *make) {
  426. make.edges.equalTo(self.bottomView);
  427. }];
  428. self.blurEffectView = blurEffectView;
  429. [self.bottomView sendSubviewToBack:blurEffectView];
  430. }
  431. }
  432. - (void)unBlurBottomBgView {
  433. self.whiteBgView.hidden = NO;
  434. if (self.blurEffectView) {
  435. [self.blurEffectView removeFromSuperview];
  436. self.blurEffectView = nil;
  437. }
  438. }
  439. #pragma mark - Setter & Getter
  440. - (void)setTitle:(NSString *)title {
  441. _title = [title copy];
  442. self.titleLabel.text = title;
  443. [self updateTitleLabel];
  444. [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
  445. make.top.equalTo(self.titleLabel.mas_bottom).offset(self.title.length > 0 ? self.titleEdgeInsets.bottom : 0);
  446. }];
  447. self.lineView.hidden = !self.title || self.title.length == 0;
  448. }
  449. - (void)setCancelButtonTitle:(NSString *)cancelButtonTitle {
  450. _cancelButtonTitle = [cancelButtonTitle copy];
  451. [self.cancelButton setTitle:cancelButtonTitle forState:UIControlStateNormal];
  452. [self updateCancelButton];
  453. }
  454. - (void)setDestructiveButtonIndexSet:(NSIndexSet *)destructiveButtonIndexSet {
  455. _destructiveButtonIndexSet = destructiveButtonIndexSet;
  456. if ([destructiveButtonIndexSet containsIndex:0]) {
  457. [self.cancelButton setTitleColor:self.destructiveButtonColor forState:UIControlStateNormal];
  458. } else {
  459. [self.cancelButton setTitleColor:self.buttonColor forState:UIControlStateNormal];
  460. }
  461. [self.tableView reloadData];
  462. }
  463. - (void)setRedButtonIndexSet:(NSIndexSet *)redButtonIndexSet {
  464. self.destructiveButtonIndexSet = redButtonIndexSet;
  465. }
  466. - (NSIndexSet *)redButtonIndexSet {
  467. return self.destructiveButtonIndexSet;
  468. }
  469. - (void)setUnBlur:(BOOL)unBlur {
  470. _unBlur = unBlur;
  471. if (unBlur) {
  472. [self unBlurBottomBgView];
  473. } else {
  474. [self blurBottomBgView];
  475. }
  476. }
  477. - (void)setBlurEffectStyle:(UIBlurEffectStyle)blurEffectStyle {
  478. _blurEffectStyle = blurEffectStyle;
  479. [self unBlurBottomBgView];
  480. [self blurBottomBgView];
  481. }
  482. - (void)setTitleFont:(UIFont *)aTitleFont {
  483. _titleFont = aTitleFont;
  484. self.titleLabel.font = aTitleFont;
  485. [self updateBottomView];
  486. [self updateTitleLabel];
  487. }
  488. - (void)setButtonFont:(UIFont *)aButtonFont {
  489. _buttonFont = aButtonFont;
  490. self.cancelButton.titleLabel.font = aButtonFont;
  491. [self.tableView reloadData];
  492. }
  493. - (void)setDestructiveButtonColor:(UIColor *)aDestructiveButtonColor {
  494. _destructiveButtonColor = aDestructiveButtonColor;
  495. if ([self.destructiveButtonIndexSet containsIndex:0]) {
  496. [self.cancelButton setTitleColor:self.destructiveButtonColor forState:UIControlStateNormal];
  497. } else {
  498. [self.cancelButton setTitleColor:self.buttonColor forState:UIControlStateNormal];
  499. }
  500. [self.tableView reloadData];
  501. }
  502. - (void)setTitleColor:(UIColor *)aTitleColor {
  503. _titleColor = aTitleColor;
  504. self.titleLabel.textColor = aTitleColor;
  505. }
  506. - (void)setButtonColor:(UIColor *)aButtonColor {
  507. _buttonColor = aButtonColor;
  508. [self.cancelButton setTitleColor:aButtonColor forState:UIControlStateNormal];
  509. [self.tableView reloadData];
  510. }
  511. - (void)setButtonHeight:(CGFloat)aButtonHeight {
  512. _buttonHeight = aButtonHeight;
  513. [self.tableView reloadData];
  514. [self updateBottomView];
  515. [self updateTableView];
  516. [self updateCancelButton];
  517. }
  518. - (NSInteger)cancelButtonIndex {
  519. return 0;
  520. }
  521. - (void)setScrolling:(BOOL)scrolling {
  522. _scrolling = scrolling;
  523. [self updateBottomView];
  524. [self updateTableView];
  525. }
  526. - (void)setVisibleButtonCount:(CGFloat)visibleButtonCount {
  527. _visibleButtonCount = visibleButtonCount;
  528. [self updateBottomView];
  529. [self updateTableView];
  530. }
  531. - (void)setTitleEdgeInsets:(UIEdgeInsets)titleEdgeInsets {
  532. _titleEdgeInsets = titleEdgeInsets;
  533. [self updateBottomView];
  534. [self updateTitleLabel];
  535. [self updateTableView];
  536. }
  537. - (void)setSeparatorColor:(UIColor *)separatorColor {
  538. _separatorColor = separatorColor;
  539. self.lineView.backgroundColor = separatorColor;
  540. self.divisionView.backgroundColor = separatorColor;
  541. [self.cancelButton setBackgroundImage:[UIImage lc_imageWithColor:separatorColor]
  542. forState:UIControlStateHighlighted];
  543. [self.tableView reloadData];
  544. }
  545. - (CGSize)titleTextSize {
  546. CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width -
  547. (self.titleEdgeInsets.left + self.titleEdgeInsets.right),
  548. MAXFLOAT);
  549. NSStringDrawingOptions opts =
  550. NSStringDrawingUsesLineFragmentOrigin |
  551. NSStringDrawingUsesFontLeading;
  552. NSDictionary *attrs = @{NSFontAttributeName : self.titleFont};
  553. _titleTextSize =
  554. [self.title boundingRectWithSize:size
  555. options:opts
  556. attributes:attrs
  557. context:nil].size;
  558. return _titleTextSize;
  559. }
  560. #pragma mark - Update Views
  561. - (void)updateBottomView {
  562. [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
  563. CGFloat height = (self.title.length > 0 ? self.titleTextSize.height + 2.0f + (self.titleEdgeInsets.top + self.titleEdgeInsets.bottom) : 0) + (self.otherButtonTitles.count > 0 ? (self.canScrolling ? MIN(self.visibleButtonCount, self.otherButtonTitles.count) : self.otherButtonTitles.count) * self.buttonHeight : 0) + (self.cancelButtonTitle.length > 0 ? LCCancelButtonTopMargin + self.buttonHeight : 0);
  564. make.height.equalTo(@(height));
  565. }];
  566. }
  567. - (void)updateTitleLabel {
  568. [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  569. make.top.equalTo(self.bottomView).offset(self.title.length > 0 ? self.titleEdgeInsets.top : 0);
  570. make.left.equalTo(self.bottomView).offset(self.titleEdgeInsets.left);
  571. make.right.equalTo(self.bottomView).offset(-self.titleEdgeInsets.right);
  572. CGFloat height = self.title.length > 0 ? self.titleTextSize.height + 2.0f : 0; // Prevent omit
  573. make.height.equalTo(@(height));
  574. }];
  575. }
  576. - (void)updateTableView {
  577. if (!self.canScrolling) {
  578. [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
  579. make.height.equalTo(@(self.otherButtonTitles.count * self.buttonHeight));
  580. make.top.equalTo(self.titleLabel.mas_bottom).offset(self.title.length > 0 ? self.titleEdgeInsets.bottom : 0);
  581. }];
  582. } else {
  583. [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
  584. make.height.equalTo(@(MIN(self.visibleButtonCount, self.otherButtonTitles.count) * self.buttonHeight));
  585. make.top.equalTo(self.titleLabel.mas_bottom).offset(self.title.length > 0 ? self.titleEdgeInsets.bottom : 0);
  586. }];
  587. }
  588. }
  589. - (void)updateCancelButton {
  590. [self.divisionView mas_updateConstraints:^(MASConstraintMaker *make) {
  591. CGFloat height = self.cancelButtonTitle.length > 0 ? LCCancelButtonTopMargin : 0;
  592. make.height.equalTo(@(height));
  593. }];
  594. [self.cancelButton mas_updateConstraints:^(MASConstraintMaker *make) {
  595. CGFloat height = self.cancelButtonTitle.length > 0 ? self.buttonHeight : 0;
  596. make.height.equalTo(@(height));
  597. }];
  598. }
  599. #pragma mark - Show & Hide
  600. - (void)show {
  601. if ([self.delegate respondsToSelector:@selector(willPresentActionSheet:)]) {
  602. [self.delegate willPresentActionSheet:self];
  603. }
  604. if (self.willPresentHandler) {
  605. self.willPresentHandler(self);
  606. }
  607. LCActionSheetViewController *viewController = [[LCActionSheetViewController alloc] init];
  608. viewController.statusBarStyle = [UIApplication sharedApplication].statusBarStyle;
  609. UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  610. window.windowLevel = UIWindowLevelStatusBar;
  611. window.rootViewController = viewController;
  612. [window makeKeyAndVisible];
  613. self.window = window;
  614. [viewController.view addSubview:self];
  615. [self mas_makeConstraints:^(MASConstraintMaker *make) {
  616. make.edges.equalTo(viewController.view);
  617. }];
  618. [self.window layoutIfNeeded];
  619. __weak typeof(self) weakSelf = self;
  620. [UIView animateWithDuration:self.animationDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  621. __strong typeof(weakSelf) strongSelf = weakSelf;
  622. strongSelf.darkView.alpha = strongSelf.darkOpacity;
  623. strongSelf.darkView.userInteractionEnabled = !strongSelf.darkViewNoTaped;
  624. [strongSelf.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
  625. make.bottom.equalTo(strongSelf);
  626. }];
  627. [strongSelf layoutIfNeeded];
  628. } completion:^(BOOL finished) {
  629. __strong typeof(weakSelf) strongSelf = weakSelf;
  630. if ([strongSelf.delegate respondsToSelector:@selector(didPresentActionSheet:)]) {
  631. [strongSelf.delegate didPresentActionSheet:strongSelf];
  632. }
  633. if (strongSelf.didPresentHandler) {
  634. strongSelf.didPresentHandler(strongSelf);
  635. }
  636. }];
  637. }
  638. - (void)hideWithButtonIndex:(NSInteger)buttonIndex {
  639. if ([self.delegate respondsToSelector:@selector(actionSheet:willDismissWithButtonIndex:)]) {
  640. [self.delegate actionSheet:self willDismissWithButtonIndex:buttonIndex];
  641. }
  642. if (self.willDismissHandler) {
  643. self.willDismissHandler(self, buttonIndex);
  644. }
  645. __weak typeof(self) weakSelf = self;
  646. [UIView animateWithDuration:self.animationDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  647. __strong typeof(weakSelf) strongSelf = weakSelf;
  648. strongSelf.darkView.alpha = 0;
  649. strongSelf.darkView.userInteractionEnabled = NO;
  650. [strongSelf.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
  651. CGFloat height = (strongSelf.title.length > 0 ? strongSelf.titleTextSize.height + 2.0f + (strongSelf.titleEdgeInsets.top + strongSelf.titleEdgeInsets.bottom) : 0) + (strongSelf.otherButtonTitles.count > 0 ? (strongSelf.canScrolling ? MIN(strongSelf.visibleButtonCount, strongSelf.otherButtonTitles.count) : strongSelf.otherButtonTitles.count) * strongSelf.buttonHeight : 0) + (strongSelf.cancelButtonTitle.length > 0 ? LCCancelButtonTopMargin + strongSelf.buttonHeight : 0);
  652. make.bottom.equalTo(strongSelf).offset(height);
  653. }];
  654. [strongSelf layoutIfNeeded];
  655. } completion:^(BOOL finished) {
  656. __strong typeof(weakSelf) strongSelf = weakSelf;
  657. [strongSelf removeFromSuperview];
  658. strongSelf.window.rootViewController = nil;
  659. strongSelf.window.hidden = YES;
  660. strongSelf.window = nil;
  661. if ([strongSelf.delegate respondsToSelector:@selector(actionSheet:didDismissWithButtonIndex:)]) {
  662. [strongSelf.delegate actionSheet:strongSelf didDismissWithButtonIndex:buttonIndex];
  663. }
  664. if (strongSelf.didDismissHandler) {
  665. strongSelf.didDismissHandler(strongSelf, buttonIndex);
  666. }
  667. }];
  668. }
  669. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  670. if (!self.canScrolling) {
  671. scrollView.contentOffset = CGPointMake(0, 0);
  672. }
  673. }
  674. #pragma mark - LCActionSheet & UITableView Delegate
  675. - (void)darkViewClicked {
  676. [self cancelButtonClicked];
  677. }
  678. - (void)cancelButtonClicked {
  679. if ([self.delegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) {
  680. [self.delegate actionSheet:self clickedButtonAtIndex:0];
  681. }
  682. if (self.clickedHandler) {
  683. self.clickedHandler(self, 0);
  684. }
  685. [self hideWithButtonIndex:0];
  686. }
  687. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  688. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  689. if ([self.delegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) {
  690. [self.delegate actionSheet:self clickedButtonAtIndex:indexPath.row + 1];
  691. }
  692. if (self.clickedHandler) {
  693. self.clickedHandler(self, indexPath.row + 1);
  694. }
  695. [self hideWithButtonIndex:indexPath.row + 1];
  696. }
  697. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  698. return self.otherButtonTitles.count;
  699. }
  700. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  701. static NSString *cellID = @"LCActionSheetCell";
  702. LCActionSheetCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
  703. if (!cell) {
  704. cell = [[LCActionSheetCell alloc] initWithStyle:UITableViewCellStyleDefault
  705. reuseIdentifier:cellID];
  706. }
  707. cell.titleLabel.font = self.buttonFont;
  708. cell.titleLabel.textColor = self.buttonColor;
  709. cell.titleLabel.text = self.otherButtonTitles[indexPath.row];
  710. cell.cellSeparatorColor = self.separatorColor;
  711. // cell.lineView.hidden = indexPath.row == MAX(self.otherButtonTitles.count - 1, 0);
  712. if (indexPath.row == MAX(self.otherButtonTitles.count - 1, 0)) {
  713. cell.tag = LC_ACTION_SHEET_CELL_HIDDE_LINE_TAG;
  714. } else {
  715. cell.tag = LC_ACTION_SHEET_CELL_NO_HIDDE_LINE_TAG;
  716. }
  717. if (self.destructiveButtonIndexSet) {
  718. if ([self.destructiveButtonIndexSet containsIndex:indexPath.row + 1]) {
  719. cell.titleLabel.textColor = self.destructiveButtonColor;
  720. } else {
  721. cell.titleLabel.textColor = self.buttonColor;
  722. }
  723. }
  724. return cell;
  725. }
  726. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  727. return self.buttonHeight;
  728. }
  729. @end