UIButton没有接收UIScrollView背后的操作

时间:2012-10-04 14:09:00

标签: uitableview uiscrollview uibutton uiresponder touch-up-inside

我将UIButton放在UITableViewCell UITableView后面的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];
}

2 个答案:

答案 0 :(得分:4)

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

在你的UIScrollView子类重写 - (BOOL)pointInside:(CGPoint)指向withEvent:(UIEvent *)事件方法,使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;
}

它对我有用

答案 1 :(得分:0)

由于您已在UIButton上添加了滚动视图,因此所有触摸操作都不会传递给按钮。

    [yourScrollView setUserInteractionEnabled:NO];

这可以解决您的问题。