如何通过滚动视图传递触摸

时间:2012-10-24 06:09:33

标签: iphone touch scrollview swipe hittest

enter image description here

我在滚动视图上有3个表视图,位于x = 0.0,x = 320.0和x = 640.0。 当用户水平滑动tableviews(因为它们在顶部)时,我想将滑动事件传递给它的superview,当用户垂直滑动tableviews时,tableview必须垂直滚动。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分: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;
    }
}

就是这样,

答案 1 :(得分:0)

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