0

我正在创建一个容器视图控制器,其中子视图控制器显示为分页滚动视图。在这个容器控制器中,我想用两根手指在水平滚动的页面之间进行切换。所以我使用了 UIScrollView 并且我已经这样设置了:

self.scrollView.showsHorizontalScrollIndicator = NO;  
self.scrollView.showsVerticalScrollIndicator = NO;  
self.scrollView.delegate = self;

[self.scrollView setContentOffset:[self rectForPage:1].origin];

for (UIGestureRecognizer *gestureRecognizer in self.scrollView.gestureRecognizers) {
    if ([gestureRecognizer  isKindOfClass:[UIPanGestureRecognizer class]]) {
        UIPanGestureRecognizer *panGR = (UIPanGestureRecognizer *) gestureRecognizer;
        panGR.minimumNumberOfTouches = 2;
    }
}

我也实现了- (void)scrollViewDidScroll:(UIScrollView *)sender避免垂直滚动的方法

 - (void)scrollViewDidScroll:(UIScrollView *)sender {
     CGFloat pageWidth = self.scrollView.frame.size.width;
     int page = floor((self.scrollView.contentOffset.x +pageWidth/2) / pageWidth);
     self.pageControl.currentPage = page;
     [self.scrollView setContentOffset: CGPointMake(self.scrollView.contentOffset.x, 0)];
 }

现在的问题是 childViewControllers 子视图无法接收来自用户的触摸。我希望通过单次触摸来传递视图层次结构。我已阅读有关子类化 UIScrollView 以覆盖该方法- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view的信息,但仅在子视图为 UIControl 时才调用它。我该怎么做?

目的是这个 ChildViewControllers 里面有 UIButtons 和应该与 singleTouches 一起使用的 ScrollViews

4

1 回答 1

0

也许不完全是,但这可能会对你有所帮助。创建一个滚动视图子类并实现它;

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    // UIView will be "transparent" for touch events if we return NO
    for (id subview in self.subviews) {
        if ([subview isKindOfClass:[YourViewController class]]) {
            YourViewController *VC = (YourViewController*) subview;

            if (CGRectContainsPoint(YourViewController.frame, point)) {
                return YES;
            }
        }
    }
    return NO;
}
于 2013-05-25T00:43:15.280 回答