iPhone - 在子视图之前捕获水平滑动

发布于 2024-09-14 06:00:57 字数 673 浏览 2 评论 0原文

我有一个视图,就是能够返回到之前的视图。

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

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

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

知道如何解决这个问题吗?我尝试了大约 5 种不同的方法,每一种都让我伤透了脑筋,也失去了动力:/ 最后一个(也是最简单的一个),只是将另一个子视图添加到调查问卷视图(控制器),覆盖问题+答案视图。

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

我有点迷失了,并且正在考虑有一天编写自己的 Scrollview,因为 UIScrollView 是 iPhone 开发者面临的最可怕的怪物 :P

请注意,我支持 iPhone OS 3.0 和了,所以新的手势 API 是行不通的。

I have a view, that is able to go back to the previous view.

Let's say this is a questionnaire. So my main view is the questionnaireView(Controller), and it has a subview which shows a question with 2 possible answers. When one answers the question, one presses next, and the questionnaireView(Controller) shows the next question in that particular subview. Simple, right?

Okay, now imagine having to accomodate up to 10 anwers for a particular question. This will require implementing a scrollview in the subview, to accomodate for the question + all answers.

Now my question: I want questionnaireView(Controller) to receive notice of horizontal swipes (next/previous question), but I want all other touches (taps, for the radiobuttons of the answers, and vertical swipes for the scrollview) to go through...

Any idea's how to go about this? I have tried about 5 different approaches and broke my head and motivation on each on of 'em :/
Last one (and most simple one), was simply adding another subview to questionnaireView(Controller), overlaying the question+answer view.

This kindly catches all the touchesBegan/Moved/Ended/Cancelled for me, but even if I just put a forward in -each- of thoses methods ([self nextResponder] ...) the underlying view (with the answers, and the scrollview) won't respond anymore...

I'm kinda lost on this, and am thinking of writing my own Scrollview someday, since UIScrollView is the most terrible monster faced by iPhone devvers :P

Mind you, I am supporting iPhone OS 3.0 and up, so the new gesture APIs are no-go.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤星 2024-09-21 06:00:57

我不确定你所说的“每种方法中的前进”是什么意思。我不确定 nextResponder 是将它们转发给的正确选择。

问题在于,触摸在其整个生命周期中应该由单个视图“拥有”。我不确定 UIScrollView 或手势识别器是如何实现的,但我很确定它们所做的事情比您自己应该做的更多。

我将覆盖 QuestionnaireView 的 hitTest:withEvent:返回 self。在touchesBegan:withEvent:中,调用[super hitTest:[touch locationInView:self] withEvent:event]并存储“拥有”它的子视图。在touchesMoved:withEvent:中,将触摸转发到相关的子视图,但如果检测到手势,“忘记”拥有触摸的子视图,而是调用touchesCancelled:withEvent:。在touchesEnded/Cancelled:withEvent:中,将其转发到子视图,然后忘记所属子视图。

这很恶心,但大多数情况下都有效。有些事情会出错(从子视图的角度来看):

  • -[UIEvent attemptsForView:] 将返回 QuestionnaireView。
  • UITouch.view 将返回 QuestionnaireView
  • UITouch.phase 可能不是 TouchCancelled:withEvent: 中的 UITouchPhaseCancelled (如果您检测到手势并取消触摸)。
    *

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).
    *
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文