【问题标题】:UIViewController is not receiving touchesBegan messageUIViewController 没有收到 touchesBegan 消息
【发布时间】:2012-06-03 08:40:34
【问题描述】:

我的 UIViewController 中有一个 UIScrollView。我需要检测任何形式的触摸,然后做点什么。我还需要什么?

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"TOUCHED"); // never happens
    for (UITouch *touch in touches) {
        if ( [touch view] == self.myScrollView)
        {
            //do something
        }
    } 
}

【问题讨论】:

  • 滚动视图是否覆盖了整个视图控制器?如果是这样,那么它在响应者链中是第一个。
  • 不,它只是在视图的一部分。
  • 如何让滚动视图将触摸事件转发到视图控制器?
  • 实际上在视图控制器上的任何地方都没有检测到触摸,即使在不是滚动视图的视图上也是如此。
  • 你确实启用了触摸功能,对吧?

标签: ios uiviewcontroller uiscrollview touch


【解决方案1】:

使用控制器在UIView 类上将UserInteractionEnabled 属性设置为true

试试imgTouchMe.UserInteractionEnabled = true;

或关注

【讨论】:

    【解决方案2】:

    两个选择:

    1. 继承 UIScrollView 并实现您的 touchesBegan[...] 代码 那里
    2. 将 UIView 添加到 UIScrollView 并使用 UIView 的 touchesBegan[...] 委托方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 2011-09-07
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多