超出裁剪边界的UIScrollview子视图无法接收触摸事件

8
我有一个UIScrollView,我已经设置为每次滑动一列(每页两列) - 通过将框架设置为视图实际宽度的一半,将clipToBounds设置为NO并使用hitTest声明框架外的区域属于UIScrollView(见下面的示例)。这很好用,但是我的问题现在是UIScrollView的子视图不会收到任何触摸事件 - 只有主要的UIScrollView会。
在下面的示例中,如果包括hitTest代码,则滚动视图会正确滚动,每次分页一列,所有内容都可以看到 - 但内部scrollviews不会接收触摸事件。
如果我删除hitTest代码,则只有第一个子scrollview会接收触摸,所有内容都可以看到 - 但主滚动视图不会在非剪切区域接收到触摸。
我该怎么解决这个问题?
示例:
//=========================================
// UIScrollViewEx
// Just in order to log touches...
//=========================================

@interface UIScrollViewEx : UIScrollView {} 
@end

@implementation UIScrollViewEx
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touches Began (0x%08X)", (unsigned int)self);
}
@end

//=========================================
// UIViewEx
// Dummy class - sets subview as hit target
// just to demonstrate usage of non-clipped 
// content
//=========================================

@interface UIViewEx : UIView {} 
@end

@implementation UIViewEx
- (UIView *) hitTest:(CGPoint) point withEvent:(UIEvent *)event {
    if ([self pointInside:point withEvent:event]) {
        return [self.subviews objectAtIndex:0];
    }
    return nil;
}
@end

//=========================================
// MainClass
// Any UIViewEx based class which returns
// the UIScrollView child on hittest
//=========================================

@implementation MyClass

- (UIColor*) randomColor
{
    int r = arc4random() % 100;
    int g = arc4random() % 100;
    int b = arc4random() % 100;
    return [UIColor colorWithRed:(0.01 * r) green:(0.01 * g) blue:(0.01 * b) alpha:1.0];
}

- (void) loadScrollviews
{
    // Set frame to half of actual width so that paging will swipe half a page only
    CGRect frame = CGRectMake(0, 0, self.bounds.size.width / 2, 400);

    // Main scrollview
    UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:frame];
    [scrollview setBackgroundColor:[UIColor greenColor]];
    [scrollview setPagingEnabled:YES];
    [scrollview setClipsToBounds:NO];

    // Create smaller scrollviews inside it - each one half a screen wide
    const int numItems = 5;
    for(int i = 0; i < numItems; ++i) {
        frame.origin.x = frame.size.width * i;
        UIScrollView *innerScrollview = [[UIScrollViewEx alloc] initWithFrame:frame];
        [innerScrollview setContentSize:CGSizeMake(frame.size.width, 1000)];
        [innerScrollview setBackgroundColor:[self randomColor]];
        [scrollview addSubview:innerScrollview];
        [innerScrollview release];
    }

    [scrollview setContentSize:CGSizeMake(numItems * frame.size.width, frame.size.height)];

    [self addSubview:scrollview];
}

@end
更新
我通过以下方式将触摸事件转发到内部视图,但肯定还有更好的方法吧?
- (UIView *) hitTest: (CGPoint) pt withEvent: (UIEvent *) event 
{    
    if(CGRectContainsPoint(self.bounds, pt)) 
    {
        UIScrollView *scrollview = [self.subviews objectAtIndex:0];
        CGPoint scrollViewpoint = [scrollview convertPoint:pt fromView:self];

        for(UIView *view in scrollview.subviews) {
            if(CGRectContainsPoint(view.frame, scrollViewpoint)) {
                return view;
            }
        }
        return scrollview;
    } 
    else {
        return [super hitTest:pt withEvent:event];
    }
}

我发现它类似于这个(https://dev59.com/52865IYBdhLWcg3wl_s3),但问题仍然存在。 - LK.
1个回答

10

这个也许可行:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event {
    UIView* child = nil;
    if ((child = [super hitTest:point withEvent:event]) == self)
        return self.scrollView;         
    return child;
}

但是,如果子视图超出滚动视图边界,则不会触发事件,并且此函数将返回self.scrollView。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接