首页 > 解决方案 > 接收 touchesBegan:在 UIViewController - Objective C

问题描述

我在 UITableView 上添加了一个 UITableView 作为子视图。我在 UITableView (子视图)上的“传递”触摸到 UIViewController 的视图(我的 touchesBegan: 方法按预期工作)的视图令人不安。

我努力了:

另外:在我的 tableView 上禁用 userInteraction 有效,但当然,我仍然想维护 cellDidSelectRow.. 功能。

我在 ViewController 中的代码实现:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesBegan popup");
    if (!self.draggable) {
        //[super touchesBegan:touches withEvent:event];
        return;
    }

    UITouch *touch = [touches anyObject];
    if (!_moving) {
    //(touch.view == self || touch.view.superview == self) &&
        _moving = YES;
        _movingStartY = [touch locationInView:self.view].y;
        NSLog(@"moving popup");
    }

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    if (!self.draggable) {
        //[super touchesMoved:touches withEvent:event];
        return;
    }

    if (_moving) {
        NSLog(@"touchesMoved popup");
        UITouch *touch = [touches anyObject];
        float offset = [touch locationInView:self.view].y - _movingStartY;
        if ([self.touchEventDelegate respondsToSelector:@selector(popupNavigationBar:touchDidMoveWithOffset:)]) {
            [self.touchEventDelegate popupNavigationBar:self touchDidMoveWithOffset:offset];
        }
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.draggable) {
        //[super touchesCancelled:touches withEvent:event];
        return;
    }

    if (_moving) {
        UITouch *touch = [touches anyObject];
        float offset = [touch locationInView:self.view].y - _movingStartY;
        [self movingDidEndWithOffset:offset];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.draggable) {
        //[self touchesEnded:touches withEvent:event];
        return;
    }

    if (_moving) {
        UITouch *touch = [touches anyObject];
        float offset = [touch locationInView:self.view].y - _movingStartY;
        [self movingDidEndWithOffset:offset];
    }
}

- (void)movingDidEndWithOffset:(float)offset
{
    _moving = NO;
    if ([self.touchEventDelegate respondsToSelector:@selector(popupNavigationBar:touchDidEndWithOffset:)]) {
        [self.touchEventDelegate popupNavigationBar:self touchDidEndWithOffset:offset];
    }
}

我怎样才能做到这一点?

标签: iosobjective-cuitableviewcocoa-touch

解决方案


推荐阅读