2
0

DNImagePickerController.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // DNImagePickerController.m
  3. // ImagePicker
  4. //
  5. // Created by DingXiao on 15/2/10.
  6. // Copyright (c) 2015年 Dennis. All rights reserved.
  7. //
  8. #import "DNImagePickerController.h"
  9. #import "DNAlbumTableViewController.h"
  10. #import "DNImageFlowViewController.h"
  11. #import "DNImagePickerHelper.h"
  12. NSString *kDNImagePickerStoredGroupKey = @"com.dennis.kDNImagePickerStoredGroup";
  13. @interface DNImagePickerController ()<UIGestureRecognizerDelegate, UINavigationControllerDelegate>
  14. @property (nonatomic, weak) id<UINavigationControllerDelegate> navDelegate;
  15. @property (nonatomic, assign) BOOL isDuringPushAnimation;
  16. @end
  17. @implementation DNImagePickerController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. if (!self.delegate) {
  21. self.delegate = self;
  22. }
  23. self.interactivePopGestureRecognizer.delegate = self;
  24. NSString *propwetyID = [[NSUserDefaults standardUserDefaults] objectForKey:kDNImagePickerStoredGroupKey];
  25. if (propwetyID.length <= 0) {
  26. [self showAlbumList];
  27. } else {
  28. [self showImageFlow];
  29. }
  30. }
  31. - (void)didReceiveMemoryWarning {
  32. [super didReceiveMemoryWarning];
  33. // Dispose of any resources that can be recreated.
  34. }
  35. #pragma mark - priviate methods
  36. - (void)showAlbumList {
  37. DNAlbumTableViewController *albumTableViewController = [[DNAlbumTableViewController alloc] init];
  38. [self setViewControllers:@[albumTableViewController]];
  39. }
  40. - (void)showImageFlow {
  41. NSString *albumIdentifier = [DNImagePickerHelper albumIdentifier];
  42. DNAlbumTableViewController *albumTableViewController = [[DNAlbumTableViewController alloc] init];
  43. DNImageFlowViewController *imageFlowController = [[DNImageFlowViewController alloc] initWithAlbumIdentifier:albumIdentifier];
  44. [self setViewControllers:@[albumTableViewController,imageFlowController]];
  45. }
  46. - (void)chargeAuthorizationStatus:(DNAlbumAuthorizationStatus)status {
  47. if (!self.viewControllers.firstObject) {
  48. [self showAlbumList];
  49. }
  50. DNAlbumTableViewController *albumTableViewController = self.viewControllers.firstObject;
  51. switch (status) {
  52. case DNAlbumAuthorizationStatusAuthorized:
  53. // TODO:Authorized
  54. break;
  55. case DNAlbumAuthorizationStatusDenied:
  56. case DNAlbumAuthorizationStatusRestricted:
  57. [albumTableViewController showUnAuthorizedTipsView];
  58. break;
  59. case DNAlbumAuthorizationStatusNotDetermined:
  60. // TODO: requestAuthorization
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. #pragma mark - UINavigationController
  67. - (void)setDelegate:(id<UINavigationControllerDelegate>)delegate
  68. {
  69. [super setDelegate:delegate ? self : nil];
  70. self.navDelegate = delegate != self ? delegate : nil;
  71. }
  72. - (void)pushViewController:(UIViewController *)viewController
  73. animated:(BOOL)animated __attribute__((objc_requires_super))
  74. {
  75. self.isDuringPushAnimation = YES;
  76. [super pushViewController:viewController animated:animated];
  77. }
  78. #pragma mark UINavigationControllerDelegate
  79. - (void)navigationController:(UINavigationController *)navigationController
  80. didShowViewController:(UIViewController *)viewController
  81. animated:(BOOL)animated
  82. {
  83. self.isDuringPushAnimation = NO;
  84. if ([self.navDelegate respondsToSelector:_cmd]) {
  85. [self.navDelegate navigationController:navigationController didShowViewController:viewController animated:animated];
  86. }
  87. }
  88. #pragma mark - UIGestureRecognizerDelegate
  89. - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
  90. {
  91. if (gestureRecognizer == self.interactivePopGestureRecognizer) {
  92. return [self.viewControllers count] > 1 && !self.isDuringPushAnimation;
  93. } else {
  94. return YES;
  95. }
  96. }
  97. #pragma mark - Delegate Forwarder
  98. - (BOOL)respondsToSelector:(SEL)s {
  99. return [super respondsToSelector:s] || [self.navDelegate respondsToSelector:s];
  100. }
  101. - (NSMethodSignature *)methodSignatureForSelector:(SEL)s {
  102. return [super methodSignatureForSelector:s] ?: [(id)self.navDelegate methodSignatureForSelector:s];
  103. }
  104. - (void)forwardInvocation:(NSInvocation *)invocation {
  105. id delegate = self.navDelegate;
  106. if ([delegate respondsToSelector:invocation.selector]) {
  107. [invocation invokeWithTarget:delegate];
  108. }
  109. }
  110. @end