【问题标题】:How to pass the touches to view from scrollview如何从滚动视图传递触摸以查看
【发布时间】:2012-10-24 06:09:33
【问题描述】:

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

我怎样才能做到这一点?

【问题讨论】:

    标签: iphone touch scrollview swipe hittest


    【解决方案1】:

    要传递来自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;
        }
    }
    

    就是这样,

    【讨论】:

    • 此答案使用私有 API 和全局状态,并以有问题的逻辑覆盖 UIScrollView 的内部方法,用于处理您的流程中的所有滚动视图。这个答案绝对可怕,永远不应该使用。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多