0

在此处输入图像描述

我在 x = 0.0、x = 320.0 和 x = 640.0 的滚动视图上放置了 3 个表格视图。当用户水平滑动表格视图(因为它们在顶部)时,我想将滑动事件传递给它的超级视图,并且当用户垂直滑动表格视图时,表格视图必须垂直滚动。

我怎样才能做到这一点?

4

2 回答 2

2

要从 传递触摸UIScrollView,请将此代码用作类别:

@implementation UIScrollView (FixedApi)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];

    NSLog(@"touch view = %@", [[touch view].class description]);
    if ([[[touch view].class description] isEqualToString:@"UITableViewCellContentView"]) {
        //To pass the touch to UITableViewCell
    } else if ([[[touch view].class description] isEqualToString:@"UITableView"] && isHorizntalSCroll == true && pageNumber == 2) {
        //To pass the touch to UITableView

    } else if ([[[touch view].class description] isEqualToString:@"UIView"]) {
        //To pass the touch to UIView
    } else {
    [self.superview touchesBegan:touches withEvent:event]; // or 1 nextResponder, depends
    [super touchesBegan:touches withEvent:event];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if ( !self.dragging ) [self.nextResponder.nextResponder touchesEnded:touches withEvent:event];
    [super touchesEnded:touches withEvent:event];
}

@end

要确定垂直/水平滚动,您可以使用以下代码:

.h 文件<UIScrollViewDelegate>

BOOL pageControlIsChangingPage;
CGPoint startPos;
int     scrollDirection;
int CX; //The width of the pages.
BOOL isHorizntalSCroll;
int pageNumber;

.m 文件

#pragma mark -
#pragma mark UIScrollViewDelegate stuff
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView {
    if (scrollDirection==0){//we need to determine direction
        //use the difference between positions to determine the direction.
        if (abs(startPos.x-scrollView.contentOffset.x)<abs(startPos.y-scrollView.contentOffset.y)){          
            NSLog(@"Vertical Scrolling");
            scrollDirection=1;
            isHorizntalSCroll = false;
            [scrollView setPagingEnabled:NO];
        } else {
            NSLog(@"Horitonzal Scrolling");
            scrollDirection=2;
            isHorizntalSCroll = ture;
            [scrollView setPagingEnabled:YES];
        }
    }

    if (scrollDirection==1) {
        [scrollView setContentOffset:CGPointMake(startPos.x,scrollView.contentOffset.y) animated:NO];
    } else if (scrollDirection==2){
        [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x,startPos.y) animated:NO];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView {
    if (pageControlIsChangingPage) {
        return;
    }

    CGFloat pageWidth = _scrollView.frame.size.width;
    int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
    pageNumber = page;
    NSLog(@"page number = %d",page);
    if (page == 3 || page == 0) {
        [scrollView setContentSize:CGSizeMake(CX, personalInfo.frame.size.height + 100)];
    } else {
        [scrollView setContentSize:CGSizeMake(CX, [scrollView bounds].size.height)];
    }

    pageControlIsChangingPage = NO;
}

#pragma mark -
#pragma mark PageControl stuff
- (IBAction)changePage:(id)sender {
    /*
     *  Change the scroll view
     */
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * pageControl.currentPage;
    frame.origin.y = 0;

    [scrollView scrollRectToVisible:frame animated:YES];

    /*
     *  When the animated scrolling finishings, scrollViewDidEndDecelerating will turn this off
     */
    pageControlIsChangingPage = YES;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollViews{
    startPos = scrollView.contentOffset;
    scrollDirection=0;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollViews willDecelerate:(BOOL)decelerate {
    if (decelerate) {
        scrollDirection=3;
    }
}

就是这样,

于 2012-10-24T07:11:42.400 回答
0

通过 IB 或代码禁用每个 tableview 的水平contentsize滚动并将基本滚动视图的设置为 960,heightOfTableview你应该没问题......这应该让你继续。

于 2012-10-24T07:03:40.487 回答