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

时间:2014-05-24 11:52:20

标签: ios uiscrollview

家伙! 我有一个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;
}

此时我有点无能为力,所以任何输入都会非常感激。

enter image description here

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

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

                             }];
        }
    }

}