繁体   English   中英

垂直滚动视图内的水平滚动视图不起作用

[英]Horizontal Scroll View inside Vertical Scroll view doesn't work

伙计们! 我有一个UIScrollView(主滚动视图),我只想垂直滚动。 在里面我有另一个UIScrollView(子滚动视图),它只能水平滚动。 在子Scroll View中我有两个视图。 这是一张图片来说明这一点。 我的问题是子滚动视图不会水平滚动。

我使用自动布局,但也试过:

[self.innerScrollView setDelegate:self];
[self.innerScrollView setScrollEnabled:YES];
self.innerScrollView.pagingEnabled = YES;
self.innerScrollView.contentSize = CGSizeMake(640, 300);

我还尝试从UIScrollView子类化滚动视图并使用:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

在这一点上我有点无能为力,所以任何输入都会非常感激。

在此输入图像描述

使您的内部scrollView框架宽度为320

要使scrollView可水平滚动,请使contentSize宽度大于其帧宽

在较新的iOS上,我必须实现手动逻辑才能实现这一目标。

如果我想在父滚动视图上进行垂直滚动,而我在水平滚动嵌套在其中的子滚动视图,我必须在子滚动视图上启用UIScrollView委托到我当前的类,然后使用我创建的以下逻辑:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (scrollView == childScrollView) {
        static float lastOffsetY;
        float currentOffsetY = [scrollView.panGestureRecognizer translationInView:scrollView.superview].y;
        if (scrollView.panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
            lastOffsetY = currentOffsetY;
        } else {
            float dy = currentOffsetY-lastOffsetY;
            [UIView animateWithDuration:0.1f
                                  delay:0.0f
                                options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState)
                             animations:^(void) {
                                 [parentScrollView setContentOffset:CGPointMake(parentScrollView.contentOffset.x,
                                                                          parentScrollView.contentOffset.y-dy)
                                                     animated:NO];
                             }
                             completion:^(BOOL finished) {

                             }];
        }
        lastOffsetY = currentOffsetY;
    }

}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {

    if (scrollView == childScrollView) {
        float oy = parentScrollView.contentOffset.y;
        float noy = oy;
        if (oy < 0) {
            noy = 0;
        }
        if (oy > parentScrollView.contentSize.height-parentScrollView.frame.size.height) {
            noy = parentScrollView.contentSize.height-parentScrollView.frame.size.height;
        }
        if (noy != oy) {
            [UIView animateWithDuration:0.1f
                                  delay:0.0f
                                options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState)
                             animations:^(void) {
                                 [parentScrollView setContentOffset:CGPointMake(parentScrollView.contentOffset.x,
                                                                          noy)
                                                     animated:NO];
                             }
                             completion:^(BOOL finished) {

                             }];
        }
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM