AHKActionSheet.m 22 KB

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