AHKActionSheet.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. //
  2. // AHKActionSheet.m
  3. // AHKActionSheetExample
  4. //
  5. // Created by Arkadiusz on 08-04-14.
  6. // Copyright (c) 2014 Arkadiusz Holko. All rights reserved.
  7. //
  8. // Modify by Marino Faggiana on 11/01/17.
  9. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  10. //
  11. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  12. //
  13. #import <QuartzCore/QuartzCore.h>
  14. #import "AHKActionSheet.h"
  15. #import "AHKActionSheetViewController.h"
  16. static const NSTimeInterval kDefaultAnimationDuration = 0.2f;
  17. // Length of the range at which the blurred background is being hidden when the user scrolls the tableView to the top.
  18. static const CGFloat kBlurFadeRangeSize = 200.0f;
  19. static NSString * const kCellIdentifier = @"Cell";
  20. // How much user has to scroll beyond the top of the tableView for the view to dismiss automatically.
  21. static const CGFloat kAutoDismissOffset = 80.0f;
  22. // Offset at which there's a check if the user is flicking the tableView down.
  23. static const CGFloat kFlickDownHandlingOffset = 20.0f;
  24. static const CGFloat kFlickDownMinVelocity = 2000.0f;
  25. // How much free space to leave at the top (above the tableView's contents) when there's a lot of elements. It makes this control look similar to the UIActionSheet.
  26. static const CGFloat kTopSpaceMarginFraction = 0.0f;
  27. // cancelButton's shadow height as the ratio to the cancelButton's height
  28. static const CGFloat kSpaceDivide = 5.0f;
  29. // width iPhone 7 Plus
  30. static const CGFloat maxWidth = 414.0f;
  31. /// Used for storing button configuration.
  32. @interface AHKActionSheetItem : NSObject
  33. @property (copy, nonatomic) NSString *title;
  34. @property (strong, nonatomic) UIImage *image;
  35. @property (nonatomic) AHKActionSheetButtonType type;
  36. @property (strong, nonatomic) AHKActionSheetHandler handler;
  37. @property (nonatomic, strong) UIColor *backgroundColor;
  38. @property (nonatomic) CGFloat height;
  39. @end
  40. @implementation AHKActionSheetItem
  41. @end
  42. @interface AHKActionSheet() <UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate>
  43. @property (strong, nonatomic) NSMutableArray *items;
  44. @property (weak, nonatomic, readwrite) UIWindow *previousKeyWindow;
  45. @property (strong, nonatomic) UIWindow *window;
  46. @property (weak, nonatomic) UIView *blurredBackgroundView;
  47. @property (weak, nonatomic) UITableView *tableView;
  48. @property (weak, nonatomic) UIButton *cancelButton;
  49. @end
  50. @implementation AHKActionSheet
  51. #pragma mark - Init
  52. + (void)initialize
  53. {
  54. if (self != [AHKActionSheet class]) {
  55. return;
  56. }
  57. AHKActionSheet *appearance = [self appearance];
  58. [appearance setButtonHeight:50.0f];
  59. [appearance setSeparatorHeight:5.0f];
  60. [appearance setCancelButtonHeight:44.0f];
  61. [appearance setAutomaticallyTintButtonImages:@YES];
  62. [appearance setCancelButtonTextAttributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:17.0f], NSForegroundColorAttributeName : [UIColor darkGrayColor] }];
  63. [appearance setButtonTextAttributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:17.0f]}];
  64. [appearance setDisableButtonTextAttributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:17.0f]}];
  65. [appearance setDestructiveButtonTextAttributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:17.0f], NSForegroundColorAttributeName : [UIColor redColor] }];
  66. [appearance setCancelOnPanGestureEnabled:@(NO)];
  67. [appearance setCancelOnTapEmptyAreaEnabled:@(YES)];
  68. [appearance setAnimationDuration:kDefaultAnimationDuration];
  69. }
  70. - (instancetype)initWithView:(UIView *)view title:(NSString *)title
  71. {
  72. self = [super init];
  73. if (self) {
  74. _title = [title copy];
  75. _cancelButtonTitle = @"Cancel";
  76. _view = view;
  77. if (@available(iOS 11, *)) {
  78. _bottomPadding = view.safeAreaInsets.bottom;
  79. }
  80. }
  81. return self;
  82. }
  83. - (void)dealloc
  84. {
  85. self.tableView.dataSource = nil;
  86. self.tableView.delegate = nil;
  87. }
  88. #pragma mark - UITableViewDataSource
  89. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  90. {
  91. return 1;
  92. }
  93. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  94. {
  95. return (NSInteger)[self.items count];
  96. }
  97. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  98. {
  99. UITableViewCell *cell;
  100. if (cell == nil)
  101. cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
  102. //cell.selectionStyle = UITableViewCellSelectionStyleNone;
  103. AHKActionSheetItem *item = self.items[(NSUInteger)indexPath.row];
  104. NSDictionary *attributes = nil;
  105. switch (item.type)
  106. {
  107. case AHKActionSheetButtonTypeDefault:
  108. attributes = self.buttonTextAttributes;
  109. break;
  110. case AHKActionSheetButtonTypeDisabled:
  111. attributes = self.disableButtonTextAttributes;
  112. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  113. break;
  114. case AHKActionSheetButtonTypeDestructive:
  115. attributes = self.destructiveButtonTextAttributes;
  116. break;
  117. case AHKActionSheetButtonTypeEncrypted:
  118. attributes = self.encryptedButtonTextAttributes;
  119. break;
  120. }
  121. UIImageView *imageView;
  122. if (item.type == AHKActionSheetButtonTypeDisabled) {
  123. imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, _buttonHeight/2 - (30/2), 30, 30)];
  124. imageView.backgroundColor = [UIColor clearColor];
  125. [imageView setImage:item.image];
  126. } else {
  127. imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, _buttonHeight/2 - (25/2), 25, 25)];
  128. BOOL useTemplateMode = [UIImage instancesRespondToSelector:@selector(imageWithRenderingMode:)] && [self.automaticallyTintButtonImages boolValue];
  129. imageView.image = useTemplateMode ? [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] : item.image;
  130. if ([UIImageView instancesRespondToSelector:@selector(tintColor)]){
  131. imageView.tintColor = attributes[NSForegroundColorAttributeName] ? attributes[NSForegroundColorAttributeName] : [UIColor blackColor];
  132. }
  133. }
  134. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.size.height + 5 , 0, cell.frame.size.width - cell.frame.size.height - 20, cell.frame.size.height)];
  135. NSAttributedString *attrTitle = [[NSAttributedString alloc] initWithString:item.title attributes:attributes];
  136. label.text = [NSString stringWithFormat: @"test"];
  137. label.numberOfLines = 0;
  138. label.attributedText = attrTitle;
  139. label.textAlignment = [self.buttonTextCenteringEnabled boolValue] ? NSTextAlignmentCenter : NSTextAlignmentLeft;
  140. cell.backgroundColor = item.backgroundColor;
  141. cell.selectedBackgroundView = [self createBackgroundView:tableView cell:cell forRowAtIndexPath:indexPath color:self.separatorColor];
  142. for (UIView *subview in [cell.contentView subviews])
  143. [subview removeFromSuperview];
  144. [cell.contentView addSubview:imageView];
  145. [cell.contentView addSubview:label];
  146. return cell;
  147. }
  148. #pragma mark - UITableViewDelegate
  149. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  150. {
  151. AHKActionSheetItem *item = self.items[(NSUInteger)indexPath.row];
  152. if (item.type != AHKActionSheetButtonTypeDisabled) {
  153. [self dismissAnimated:YES duration:self.animationDuration completion:item.handler];
  154. }
  155. }
  156. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  157. {
  158. AHKActionSheetItem *item = self.items[(NSUInteger)indexPath.row];
  159. return item.height;
  160. }
  161. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  162. {
  163. AHKActionSheetItem *item = self.items[(NSUInteger)indexPath.row];
  164. cell.backgroundView = [self createBackgroundView:tableView cell:cell forRowAtIndexPath:indexPath color:item.backgroundColor];
  165. }
  166. - (UIView *)createBackgroundView:(UITableView *)tableView cell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath color:(UIColor *)color
  167. {
  168. CGFloat cornerRadius = 10.f;
  169. cell.backgroundColor = UIColor.clearColor;
  170. CAShapeLayer *layer = [[CAShapeLayer alloc] init];
  171. CGMutablePathRef pathRef = CGPathCreateMutable();
  172. CGRect bounds = CGRectInset(cell.bounds, 10, 0);
  173. BOOL addLine = NO;
  174. if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
  175. CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
  176. } else if (indexPath.row == 0) {
  177. CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
  178. CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
  179. CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
  180. CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
  181. addLine = YES;
  182. } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
  183. CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
  184. CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
  185. CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
  186. CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
  187. } else {
  188. CGPathAddRect(pathRef, nil, bounds);
  189. addLine = YES;
  190. }
  191. if (addLine == YES) {
  192. CALayer *lineLayer = [[CALayer alloc] init];
  193. CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
  194. lineLayer.frame = CGRectMake(CGRectGetMinX(bounds), bounds.size.height-lineHeight, bounds.size.width, lineHeight);
  195. lineLayer.backgroundColor = tableView.separatorColor.CGColor;
  196. [layer addSublayer:lineLayer];
  197. }
  198. layer.path = pathRef;
  199. CFRelease(pathRef);
  200. layer.fillColor = color.CGColor;
  201. UIView *testView = [[UIView alloc] initWithFrame:bounds];
  202. [testView.layer insertSublayer:layer atIndex:0];
  203. testView.backgroundColor = UIColor.clearColor;
  204. return testView;
  205. }
  206. #pragma mark - UIScrollViewDelegate
  207. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  208. {
  209. if (![self.cancelOnPanGestureEnabled boolValue]) {
  210. return;
  211. }
  212. [self fadeBlursOnScrollToTop];
  213. }
  214. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  215. {
  216. if (![self.cancelOnPanGestureEnabled boolValue]) {
  217. return;
  218. }
  219. CGPoint scrollVelocity = [scrollView.panGestureRecognizer velocityInView:self];
  220. BOOL viewWasFlickedDown = scrollVelocity.y > kFlickDownMinVelocity && scrollView.contentOffset.y < -self.tableView.contentInset.top - kFlickDownHandlingOffset;
  221. BOOL shouldSlideDown = scrollView.contentOffset.y < -self.tableView.contentInset.top - kAutoDismissOffset;
  222. if (viewWasFlickedDown) {
  223. // use a shorter duration for a flick down animation
  224. static const NSTimeInterval duration = 0.2f;
  225. [self dismissAnimated:YES duration:duration completion:self.cancelHandler];
  226. } else if (shouldSlideDown) {
  227. [self dismissAnimated:YES duration:self.animationDuration completion:self.cancelHandler];
  228. }
  229. }
  230. #pragma mark - Properties
  231. - (NSMutableArray *)items
  232. {
  233. if (!_items) {
  234. _items = [NSMutableArray array];
  235. }
  236. return _items;
  237. }
  238. #pragma mark - Actions
  239. - (void)cancelButtonTapped:(id)sender
  240. {
  241. [self dismissAnimated:YES duration:self.animationDuration completion:self.cancelHandler];
  242. }
  243. #pragma mark - Public
  244. - (void)addButtonWithTitle:(NSString *)title image:(UIImage *)image backgroundColor:(UIColor *)backgroundColor height:(CGFloat)height type:(AHKActionSheetButtonType)type handler:(AHKActionSheetHandler)handler
  245. {
  246. AHKActionSheetItem *item = [[AHKActionSheetItem alloc] init];
  247. item.title = title;
  248. item.image = image;
  249. item.backgroundColor = backgroundColor;
  250. item.height = height;
  251. item.type = type;
  252. item.handler = handler;
  253. [self.items addObject:item];
  254. }
  255. - (void)show
  256. {
  257. if ([self isVisible]) {
  258. return;
  259. }
  260. self.previousKeyWindow = [UIApplication sharedApplication].keyWindow;
  261. [self setUpNewWindow];
  262. [self setUpBlurredBackground];
  263. [self setUpCancelButton];
  264. [self setUpTableView];
  265. if (self.cancelOnTapEmptyAreaEnabled.boolValue) {
  266. [self setUpCancelTapGestureForView:self.tableView];
  267. [self setUpCancelTapGestureForView:self.blurredBackgroundView];
  268. }
  269. CGFloat slideDownMinOffset = (CGFloat)fmin(CGRectGetHeight(self.frame) + self.tableView.contentOffset.y, CGRectGetHeight(self.frame));
  270. self.tableView.transform = CGAffineTransformMakeTranslation(0, slideDownMinOffset);
  271. void(^immediateAnimations)(void) = ^(void) {
  272. self.blurredBackgroundView.alpha = 1.0f;
  273. };
  274. void(^delayedAnimations)(void) = ^(void) {
  275. CGFloat width = CGRectGetWidth(self.view.bounds);
  276. if (width > maxWidth) width = maxWidth;
  277. self.cancelButton.frame = CGRectMake(10 + (CGRectGetWidth(self.view.bounds)/2 - width/2), CGRectGetMaxY(self.view.bounds) - self.cancelButtonHeight - _bottomPadding, width - 20, self.cancelButtonHeight - kSpaceDivide);
  278. // Corner Radius
  279. self.cancelButton.layer.cornerRadius = 10;
  280. self.cancelButton.clipsToBounds = YES;
  281. // Add White color background
  282. self.cancelButton.backgroundColor = [UIColor whiteColor];
  283. self.tableView.transform = CGAffineTransformMakeTranslation(0, 0);
  284. // manual calculation of table's contentSize.height
  285. CGFloat tableContentHeight = 0;
  286. for (AHKActionSheetItem *item in self.items) {
  287. tableContentHeight = tableContentHeight + item.height;
  288. }
  289. tableContentHeight = tableContentHeight + self.separatorHeight + CGRectGetHeight(self.tableView.tableHeaderView.frame);
  290. CGFloat topInset;
  291. BOOL buttonsFitInWithoutScrolling = tableContentHeight < CGRectGetHeight(self.tableView.frame) * (1.0 - kTopSpaceMarginFraction);
  292. if (buttonsFitInWithoutScrolling) {
  293. // show all buttons if there isn't many
  294. topInset = CGRectGetHeight(self.tableView.frame) - tableContentHeight;
  295. } else {
  296. // leave an empty space on the top to make the control look similar to UIActionSheet
  297. topInset = (CGFloat)round(CGRectGetHeight(self.tableView.frame) * kTopSpaceMarginFraction);
  298. }
  299. self.tableView.contentInset = UIEdgeInsetsMake(topInset, 0, 0, 0);
  300. self.tableView.bounces = [self.cancelOnPanGestureEnabled boolValue] || !buttonsFitInWithoutScrolling;
  301. };
  302. if ([UIView respondsToSelector:@selector(animateKeyframesWithDuration:delay:options:animations:completion:)]){
  303. // Animate sliding in tableView and cancel button with keyframe animation for a nicer effect.
  304. [UIView animateKeyframesWithDuration:self.animationDuration delay:0 options:0 animations:^{
  305. immediateAnimations();
  306. [UIView addKeyframeWithRelativeStartTime:0.3f relativeDuration:0.7f animations:^{
  307. delayedAnimations();
  308. }];
  309. } completion:nil];
  310. } else {
  311. [UIView animateWithDuration:self.animationDuration animations:^{
  312. immediateAnimations();
  313. delayedAnimations();
  314. }];
  315. }
  316. }
  317. - (void)dismissAnimated:(BOOL)animated
  318. {
  319. [self dismissAnimated:animated duration:self.animationDuration completion:self.cancelHandler];
  320. }
  321. #pragma mark - Private
  322. - (BOOL)isVisible
  323. {
  324. // action sheet is visible iff it's associated with a window
  325. return !!self.window;
  326. }
  327. - (void)dismissAnimated:(BOOL)animated duration:(NSTimeInterval)duration completion:(AHKActionSheetHandler)completionHandler
  328. {
  329. if (![self isVisible]) {
  330. return;
  331. }
  332. // delegate isn't needed anymore because tableView will be hidden (and we don't want delegate methods to be called now)
  333. self.tableView.delegate = nil;
  334. self.tableView.userInteractionEnabled = NO;
  335. // keep the table from scrolling back up
  336. self.tableView.contentInset = UIEdgeInsetsMake(-self.tableView.contentOffset.y, 0, 0, 0);
  337. void(^tearDownView)(void) = ^(void) {
  338. // remove the views because it's easiest to just recreate them if the action sheet is shown again
  339. for (UIView *view in @[self.tableView, self.cancelButton, self.blurredBackgroundView, self.window]) {
  340. [view removeFromSuperview];
  341. }
  342. self.window = nil;
  343. [self.previousKeyWindow makeKeyAndVisible];
  344. if (completionHandler) {
  345. completionHandler(self);
  346. }
  347. };
  348. if (animated) {
  349. // animate sliding down tableView and cancelButton.
  350. [UIView animateWithDuration:duration animations:^{
  351. self.blurredBackgroundView.alpha = 0.0f;
  352. self.cancelButton.transform = CGAffineTransformTranslate(self.cancelButton.transform, 0, self.cancelButtonHeight - kSpaceDivide + _bottomPadding);
  353. // Shortest shift of position sufficient to hide all tableView contents below the bottom margin.
  354. // contentInset isn't used here (unlike in -show) because it caused weird problems with animations in some cases.
  355. CGFloat slideDownMinOffset = (CGFloat)fmin(CGRectGetHeight(self.frame) + self.tableView.contentOffset.y, CGRectGetHeight(self.frame));
  356. self.tableView.transform = CGAffineTransformMakeTranslation(0, slideDownMinOffset);
  357. } completion:^(BOOL finished) {
  358. tearDownView();
  359. }];
  360. } else {
  361. tearDownView();
  362. }
  363. }
  364. - (void)setUpNewWindow
  365. {
  366. AHKActionSheetViewController *actionSheetVC = [[AHKActionSheetViewController alloc] initWithNibName:nil bundle:nil];
  367. actionSheetVC.actionSheet = self;
  368. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  369. self.window.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  370. self.window.opaque = NO;
  371. self.window.rootViewController = actionSheetVC;
  372. [self.window makeKeyAndVisible];
  373. }
  374. - (void)setUpBlurredBackground
  375. {
  376. UIView *backgroundView = [UIView new];
  377. backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
  378. backgroundView.frame = [UIScreen mainScreen].bounds;
  379. [self addSubview:backgroundView];
  380. self.blurredBackgroundView = backgroundView;
  381. }
  382. - (void)setUpCancelTapGestureForView:(UIView*)view {
  383. UITapGestureRecognizer *cancelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelButtonTapped:)];
  384. cancelTap.delegate = self;
  385. [view addGestureRecognizer:cancelTap];
  386. }
  387. - (void)setUpCancelButton
  388. {
  389. UIButton *cancelButton;
  390. CGFloat width = CGRectGetWidth(self.view.bounds);
  391. if (width > maxWidth) width = maxWidth;
  392. // It's hard to check if UIButtonTypeSystem enumeration exists, so we're checking existence of another method that was introduced in iOS 7.
  393. if ([UIView instancesRespondToSelector:@selector(tintAdjustmentMode)]) {
  394. cancelButton= [UIButton buttonWithType:UIButtonTypeSystem];
  395. } else {
  396. cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
  397. }
  398. NSAttributedString *attrTitle = [[NSAttributedString alloc] initWithString:self.cancelButtonTitle attributes:self.cancelButtonTextAttributes];
  399. [cancelButton setAttributedTitle:attrTitle forState:UIControlStateNormal];
  400. [cancelButton addTarget:self action:@selector(cancelButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  401. cancelButton.frame = CGRectMake(10 + (CGRectGetWidth(self.view.bounds)/2 - width/2), CGRectGetMaxY(self.view.bounds) - self.cancelButtonHeight, width - 20, self.cancelButtonHeight - kSpaceDivide);
  402. // move the button below the screen (ready to be animated -show)
  403. cancelButton.transform = CGAffineTransformMakeTranslation(0, self.cancelButtonHeight - kSpaceDivide);
  404. cancelButton.clipsToBounds = YES;
  405. [self addSubview:cancelButton];
  406. self.cancelButton = cancelButton;
  407. }
  408. - (void)setUpTableView
  409. {
  410. CGFloat width = CGRectGetWidth(self.view.bounds);
  411. if (width > maxWidth) width = maxWidth;
  412. CGRect statusBarViewRect = [self convertRect:[UIApplication sharedApplication].statusBarFrame fromView:nil];
  413. CGFloat statusBarHeight = CGRectGetHeight(statusBarViewRect);
  414. CGRect frame = CGRectMake((CGRectGetWidth(self.view.bounds)/2 - width/2), statusBarHeight, width, CGRectGetHeight(self.view.bounds) - statusBarHeight - self.cancelButtonHeight - self.separatorHeight - _bottomPadding);
  415. UITableView *tableView = [[UITableView alloc] initWithFrame:frame];
  416. tableView.backgroundColor = [UIColor clearColor];
  417. tableView.showsVerticalScrollIndicator = NO;
  418. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  419. if (self.separatorColor) {
  420. tableView.separatorColor = self.separatorColor;
  421. }
  422. tableView.delegate = self;
  423. tableView.dataSource = self;
  424. [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellIdentifier];
  425. [self insertSubview:tableView aboveSubview:self.blurredBackgroundView];
  426. // move the content below the screen, ready to be animated in -show
  427. tableView.contentInset = UIEdgeInsetsMake(CGRectGetHeight(self.view.bounds), 0, 0, 0);
  428. // removes separators below the footer (between empty cells)
  429. tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  430. self.tableView = tableView;
  431. }
  432. - (void)fadeBlursOnScrollToTop
  433. {
  434. if (self.tableView.isDragging || self.tableView.isDecelerating) {
  435. CGFloat alphaWithoutBounds = 1.0f - ( -(self.tableView.contentInset.top + self.tableView.contentOffset.y) / kBlurFadeRangeSize);
  436. // limit alpha to the interval [0, 1]
  437. CGFloat alpha = (CGFloat)fmax(fmin(alphaWithoutBounds, 1.0f), 0.0f);
  438. self.blurredBackgroundView.alpha = alpha;
  439. }
  440. }
  441. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  442. // If the view that is touched is not the view associated with this view's table view, but
  443. // is one of the sub-views, we should not recognize the touch.
  444. // Original source: http://stackoverflow.com/questions/10755566/how-to-know-uitableview-is-pressed-when-empty
  445. if (touch.view != self.tableView && [touch.view isDescendantOfView:self.tableView]) {
  446. return NO;
  447. }
  448. return YES;
  449. }
  450. @end