0

我有一个视图,即能够回到之前的视图。

假设这是一份问卷。所以我的主要视图是问卷调查视图(控制器),它有一个子视图,显示一个带有 2 个可能答案的问题。当一个人回答问题时,一个人按下下一个,并且问卷视图(控制器)在该特定子视图中显示下一个问题。很简单,对吧?

好的,现在想象一个特定问题必须容纳多达 10 个答案。这将需要在子视图中实现滚动视图,以适应问题+所有答案。

现在我的问题:我希望问卷视图(控制器)接收水平滑动的通知(下一个/上一个问题),但我希望所有其他触摸(点击,用于答案的单选按钮,以及用于滚动视图的垂直滑动)通过。 ..

有什么想法可以解决这个问题吗?我已经尝试了大约 5 种不同的方法,并且在每一个上都打破了我的头脑和动机:/ 最后一个(也是最简单的一个),只是将另一个子视图添加到问卷视图(控制器),覆盖问题+答案视图。

这很好地为我捕获了所有的 touchesBegan/Moved/Ended/Cancelled,但即使我只是在这些方法中的每一个([self nextResponder] ...)中提出一个转发,底层视图(带有答案和滚动视图) 不会再回复了...

我有点迷失了,我想有一天写我自己的 Scrollview,因为 UIScrollView 是 iPhone 开发者面临的最可怕的怪物:P

请注意,我支持 iPhone OS 3.0 及更高版本,因此新的手势 API 不可行。

4

1 回答 1

1

I'm not sure what you mean by "a forward in -each- of thoses methods". I'm not sure that nextResponder is the correct thing to forward them to either.

The problem is that touches are supposed to be "owned" by a single view throughout their lifetime. I'm not sure how UIScrollView or gesture recognizers are implemented, but I'm pretty sure they do more than you're supposed to do on your own.

I'd override questionnaireView's hitTest:withEvent: to return self. In your touchesBegan:withEvent:, call [super hitTest:[touch locationInView:self] withEvent:event] and store the subview that "owns" it. In touchesMoved:withEvent:, forward the touch to the relevant subview, but if you detect a gesture, "forget" the subview that owns the touch and instead call touchesCancelled:withEvent:. In touchesEnded/Cancelled:withEvent:, forward it to the subview and then forget the owning subview.

It's icky, but it mostly works. Some things it gets wrong (from the perspective of subviews):

  • -[UIEvent touchesForView:] will return the QuestionnaireView.
  • UITouch.view will return the QuestionnaireView
  • UITouch.phase might not be UITouchPhaseCancelled in touchesCancelled:withEvent: (if you detect the gesture and cancel the touch). *
于 2010-08-12T19:10:24.923 回答