preface

Recently, the project added new functions, encountered the need for multi-layer nested sliding effect, so I used a library written before GKPageScrollView to achieve the next, but in the implementation process also encountered a lot of problems, the most important is the problem of gesture conflict, so the library has also been updated, the following details:

rendering

First look at the effect diagram, mainly to achieve the following two layers of nesting effect

1, GKPageScrollView + Category + category

2, Sort + GKPageScrollView + sort

implementation

1, GKPageScrollView + Category + category

This is easy to implement, because the JXCategoryView itself supports multiple layers of nesting, just need to pass the corresponding listView to GKPageScrollView, the main code is as follows:

// Get the scrollView of the current sublist through JXCategoryViewDelegate#pragma mark - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    switch (index) {
        case 0:
            self.currentListScrollView = self.compListView.tableView;
            break;
        case 1:
            self.currentListScrollView = self.saleListView.tableView;
            break;
        case 2:
            self.currentListScrollView = self.priceListView.tableView;
            break;
        default:
            break; }} // Pass the scrollView of the sublist to GKPageScrollView#pragma mark - GKPageListViewDelegate
- (UIScrollView *)listScrollView {
    return self.currentListScrollView;
}
Copy the code
2, Sort + GKPageScrollView + sort

This is mainly classification nested multiple GKPageScrollView to achieve, the main code is as follows:

// Add GKNest2View CGFloat width = kScreenW; CGFloat height = kscreenh-gk_statusbar_height-50.0f; CGFloat height = kscreenh-gk_statusbar_height-50.0f; [self.categoryView.titles enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { GKNest2View *nestView = [GKNest2View new];  nestView.frame = CGRectMake(idx * width, 0, width, height); nestView.mainScrollView = self->_contentScrollView;  [self->_contentScrollView addSubview:nestView]; }]; _contentScrollView.contentSize = CGSizeMake(self.categoryView.titles.count * width, 0);Copy the code

The realization of the GKNest2View

#pragma mark - GKPageScrollViewDelegate
- (UIView *)headerViewInPageScrollView:(GKPageScrollView *)pageScrollView {
    return self.headerView;
}

- (UIView *)segmentedViewInPageScrollView:(GKPageScrollView *)pageScrollView {
    return self.categoryView;
}

- (NSInteger)numberOfListsInPageScrollView:(GKPageScrollView *)pageScrollView {
    return self.categoryView.titles.count;
}

- (id<GKPageListViewDelegate>)pageScrollView:(GKPageScrollView *)pageScrollView initListAtIndex:(NSInteger)index {
    GKNestListView *listView = [GKNestListView new];
    return listView;
}
Copy the code
3, gesture conflict resolution

The most difficult part of multi-layer nesting is the conflict of gestures. Various attempts have been made to finally solve the conflict of gestures. The main method is to implement and modify the proxy method of gestures

01. Resolution of conflicts between left and right sliding of the list container and up and down sliding of the list

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if ([self.gestureDelegate respondsToSelector:@selector(pageListContainerCollectionView:gestureRecognizerShouldBegin:)]) {
        return [self.gestureDelegate pageListContainerCollectionView:self gestureRecognizerShouldBegin:gestureRecognizer];
    }else {
        if (self.isNestEnabled) {
            if ([gestureRecognizer isMemberOfClass:NSClassFromString(@"UIScrollViewPanGestureRecognizer")]) { CGFloat velocityX = [(UIPanGestureRecognizer *)gestureRecognizer velocityInView:gestureRecognizer.view].x; // if x is greater than 0, swipe rightif (velocityX > 0) {
                    if (self.contentOffset.x == 0) {
                        returnNO; }}else if(velocityX < 0) {// if x is less than 0, slide leftif (self.contentOffset.x + self.bounds.size.width == self.contentSize.width) {
                        return NO;
                    }
                }
            }
        }
    }
    return YES;
}
Copy the code

02, GKPageScrollView sliding up and down and full screen return gesture conflict resolution

#pragma mark - GKPageTableViewGestureDelegate- (BOOL)pageTableView:(GKPageTableView *)tableView gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer ShouldRecognizeSimultaneouslyWithGestureRecognizer: (otherGestureRecognizer UIGestureRecognizer *) {/ / special processing, NSArray *internalTargets = [otherGestureRecognizer valueForKey:@"targets"];
    id internalTarget = [internalTargets.firstObject valueForKey:@"target"];
    if ([internalTarget isKindOfClass:NSClassFromString(@"_UINavigationInteractiveTransition")]) return NO;
    
    return YES;
}
Copy the code

The above is only part of the code, the main implementation or to see the specific code logic.

The last

GKPageScrollView is used in conjunction with JXCategoryView to achieve the above two nested effects. The code can be viewed in Nest in GKPageScrollView. If you feel good, please click star, your support is my biggest motivation.