AHKActionSheet.m 22 KB

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