自定义容器使用两个手指UIScrollView

时间:2013-05-25 00:32:52

标签: ios uiviewcontroller uiscrollview uigesturerecognizer

我正在创建一个容器视图控制器,其中子视图控制器显示为分页scrollView。在这个容器控制器中,我想在两个手指水平滚动的页面之间进行切换。所以我使用了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,ScrollViews应该和singleTouches一起使用

1 个答案:

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