UIButton没有接收UIScrollView背后的操作

Kub*_*lav 5 uibutton uitableview uiscrollview uiresponder touch-up-inside

我把UIButton里面UITableViewCellUITableView其后面UIScrollView.我继承UIScrollView了转发的内容UITableView.

所以方法来自UITableViewDelegate didSelectRow正确调用.问题是UIButton内部表格单元格没有接收TouchUpInside动作.

如何ScrollView在不删除的情况下解决此问题TableView

编辑:

我通过检测哪个视图将接收触摸来解决此问题.这是代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIView *hitView = [self hitTest:[(UITouch*)[[touches allObjects] objectAtIndex:0] locationInView:self] withEvent:event];
    if ([hitView isKindOfClass:[UIButton class]]) {
        [(UIButton*)hitView sendActionsForControlEvents:UIControlEventTouchUpInside];
    }
    [super touchesBegan:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)

小智 4

如果您想为两个对象启用操作 - UIScrollView 和 UIButton,您应该为 ScrollView 实现自定义命中测试机制。

在 UIScrollView 子类中重写 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 方法,使 ScrollView 后面的视图可用于获取事件。

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    return __touchesEnabled;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    _touchesEnabled = NO;
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    for(UITouch *touch in touches) {
       CGPoint point = [touch locationInView:self];
       point = [window convertPoint:point fromView:self];
       UIView *view = [window hitTest:point withEvent:event];
       [view touchesBegan:touches withEvent:event];
    }  
    _touchesEnabled = YES;
}
Run Code Online (Sandbox Code Playgroud)

这个对我有用