SegmentVController.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // SegmentVController.m
  3. // PersonalCenter
  4. //
  5. // Created by Arch on 2017/6/16.
  6. // Copyright © 2017年 mint_bin. All rights reserved.
  7. //
  8. #import "SegmentVController.h"
  9. @interface SegmentVController () <UIGestureRecognizerDelegate, UIScrollViewDelegate>
  10. @property (nonatomic, strong) UIScrollView *scrollView;
  11. @property (nonatomic, assign) BOOL canScroll;
  12. @property (nonatomic, strong) NSNumber *selectedPageIndex;
  13. @end
  14. @implementation SegmentVController
  15. #pragma mark - Life
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. //子控制器视图到达顶部的通知
  19. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:@"goTop" object:nil];
  20. //子控制器视图离开顶部的通知
  21. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:@"leaveTop" object:nil];
  22. //切换分页选项的通知
  23. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:CurrentSelectedChildViewControllerIndex object:nil];
  24. }
  25. - (void)viewDidAppear:(BOOL)animated {
  26. [super viewDidAppear:animated];
  27. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  28. self.navigationController.interactivePopGestureRecognizer.delegate = self;
  29. }
  30. }
  31. - (void)dealloc {
  32. [[NSNotificationCenter defaultCenter] removeObserver:self];
  33. }
  34. #pragma mark - Notification
  35. - (void)acceptMsg:(NSNotification *)notification {
  36. NSString *notificationName = notification.name;
  37. if ([notificationName isEqualToString:@"goTop"]) {
  38. NSDictionary *userInfo = notification.userInfo;
  39. NSString *canScroll = userInfo[@"canScroll"];
  40. if ([canScroll isEqualToString:@"1"]) {
  41. self.canScroll = YES;
  42. self.scrollView.showsVerticalScrollIndicator = YES;
  43. } else {
  44. self.canScroll = NO;
  45. }
  46. } else if ([notificationName isEqualToString:@"leaveTop"]){
  47. self.canScroll = NO;
  48. self.scrollView.contentOffset = CGPointZero;
  49. self.scrollView.showsVerticalScrollIndicator = NO;
  50. } else if ([notificationName isEqualToString:CurrentSelectedChildViewControllerIndex]) {
  51. NSDictionary *userInfo = notification.userInfo;
  52. self.selectedPageIndex = userInfo[@"selectedPageIndex"];
  53. }
  54. }
  55. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  56. if (!self.canScroll) {
  57. [scrollView setContentOffset:CGPointZero];
  58. }
  59. CGFloat offsetY = scrollView.contentOffset.y;
  60. if (offsetY <= 0) {
  61. [[NSNotificationCenter defaultCenter] postNotificationName:@"leaveTop" object:nil userInfo:@{@"canScroll":@"1"}];
  62. }
  63. self.scrollView = scrollView;
  64. }
  65. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  66. if ([self.selectedPageIndex isEqualToNumber:@0]) {
  67. return YES;
  68. }
  69. return NO;
  70. }
  71. @end