【问题标题】:Horizontal Scroll View inside Vertical Scroll view doesn't work垂直滚动视图中的水平滚动视图不起作用
【发布时间】:2014-07-13 17:25:12
【问题描述】:

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

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

[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;
}

在这一点上我有点无能为力,所以任何输入将不胜感激。

【问题讨论】:

标签: ios uiscrollview


【解决方案1】:

在较新的 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) {

                             }];
        }
    }

}

【讨论】:

    【解决方案2】:

    让你的内部 scrollView 框架宽度为 320

    为了让你的 scrollView 水平滚动,让 contentSize 的宽度大于它的框架宽度

    【讨论】:

    • 我的内部滚动视图的宽度为 640,因为它的子视图的宽度均为 320,一个从 0、0 开始,另一个从 320、0 开始。所以在内部滚动视图中,我想在这两个视图水平滚动。主滚动视图不应如图所示水平滚动。
    • 先生你需要contentSize.width 640。你有那个代码。让你的self.innerScrollView.frame.size.width320 一切正常
    • Warif Akhand Rishi,我终于理解你了,这真的解决了我的问题!!!非常感谢!!!
    猜你喜欢
    • 2015-12-21
    • 1970-01-01
    • 2013-07-18
    • 2023-04-07
    • 2015-08-04
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多