IQActionSheetViewController.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // IQActionSheetViewController.m
  3. // https://github.com/hackiftekhar/IQActionSheetPickerView
  4. // Copyright (c) 2013-14 Iftekhar Qurashi.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #import "IQActionSheetViewController.h"
  24. #import "IQActionSheetPickerView.h"
  25. @interface IQActionSheetViewController ()<UIApplicationDelegate, UIGestureRecognizerDelegate>
  26. @end
  27. @implementation IQActionSheetViewController
  28. -(void)loadView
  29. {
  30. self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  31. self.view.backgroundColor = [UIColor clearColor];
  32. UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
  33. [self.view addGestureRecognizer:tapGestureRecognizer];
  34. tapGestureRecognizer.delegate = self;
  35. }
  36. - (void) handleTapFrom: (UITapGestureRecognizer *)recognizer
  37. {
  38. if (recognizer.state == UIGestureRecognizerStateEnded)
  39. {
  40. //Code to handle the gesture
  41. if (self.pickerView.clickBackHide) {
  42. [self dismissWithCompletion:nil];
  43. }
  44. }
  45. }
  46. // MAKR: <UIGestureRecognizerDelegate>
  47. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
  48. shouldReceiveTouch:(UITouch *)touch
  49. {
  50. if (CGRectContainsPoint([self.pickerView bounds], [touch locationInView:self.pickerView]))
  51. return NO;
  52. return YES;
  53. }
  54. -(void)showPickerView:(IQActionSheetPickerView*)pickerView completion:(void (^)(void))completion
  55. {
  56. _pickerView = pickerView;
  57. // Getting topMost ViewController
  58. UIViewController *topController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
  59. while ([topController presentedViewController]) topController = [topController presentedViewController];
  60. [topController.view endEditing:YES];
  61. //Sending pickerView to bottom of the View.
  62. __block CGRect pickerViewFrame = pickerView.frame;
  63. {
  64. pickerViewFrame.origin.y = self.view.bounds.size.height;
  65. pickerView.frame = pickerViewFrame;
  66. [self.view addSubview:pickerView];
  67. }
  68. //Adding self.view to topMostController.view and adding self as childViewController to topMostController
  69. {
  70. self.view.frame = CGRectMake(0, 0, topController.view.bounds.size.width, topController.view.bounds.size.height);
  71. self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
  72. [topController addChildViewController: self];
  73. [topController.view addSubview: self.view];
  74. [self didMoveToParentViewController:topController];
  75. }
  76. //Sliding up the pickerView with animation
  77. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionBeginFromCurrentState|7<<16 animations:^{
  78. self.view.backgroundColor = self.pickerView.pickBkgColor;
  79. pickerViewFrame.origin.y = self.view.bounds.size.height-pickerViewFrame.size.height;
  80. pickerView.frame = pickerViewFrame;
  81. } completion:^(BOOL finished) {
  82. if (completion) completion();
  83. }];
  84. }
  85. -(void)dismissWithCompletion:(void (^)(void))completion
  86. {
  87. //Sliding down the pickerView with animation.
  88. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionBeginFromCurrentState|7<<16 animations:^{
  89. self.view.backgroundColor = [UIColor clearColor];
  90. CGRect pickerViewFrame = _pickerView.frame;
  91. pickerViewFrame.origin.y = self.view.bounds.size.height;
  92. _pickerView.frame = pickerViewFrame;
  93. } completion:^(BOOL finished) {
  94. //Removing pickerView from self.view
  95. [_pickerView removeFromSuperview];
  96. //Removing self.view from topMostController.view and removing self as childViewController from topMostController
  97. [self willMoveToParentViewController:nil];
  98. [self.view removeFromSuperview];
  99. [self removeFromParentViewController];
  100. if (completion) completion();
  101. }];
  102. }
  103. @end