当子视图触摸事件发生时通知视图控制器

发布于 2024-09-06 02:35:30 字数 1439 浏览 8 评论 0原文

我有一个 UIViewController,其视图有一个自定义子视图。

此自定义子视图需要跟踪触摸事件并报告滑动手势。

目前我将touchesBegan、touchesMoved、touchesEnded和touchesCancelled放在子视图类中。通过一些额外的逻辑,我可以获取滑动手势并调用我的handleRightSwipe 和handleLeftSwipe 方法。因此,现在当我在子视图内滑动时,它会调用其本地滑动处理方法。这一切都很好。

但我真正需要的是视图控制器中的handleRightSwipe 和handleLeftSwipe 方法。我可以将它们留在子视图类中,但随后我还必须引入所有逻辑和数据,这会破坏 MVC 的理念。

所以我的问题是有没有一种干净的方法来处理这个问题?本质上,我想将触摸事件方法保留在子视图中,以便它们仅针对该特定视图触发。但我还希望在发生这些触摸事件(或在本例中为滑动手势)时通知视图控制器。

有什么想法吗?

谢谢。

更新:

使用 Henrik 的建议,这里是我所做的一个快速示例(以节省您的阅读时间):

我将视图控制器设置为通知的观察者(早期)。

// NOTIFICATION_LEFT_SWIPE is defined as some unique string elsewhere.
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

// Note that imageView is the instance of my subview that is calling the notification.
// You can set this to nil if you don't want it to be specific.
[nc addObserver:self selector:@selector(handleLeftSwipe) name:@NOTIFICATION_LEFT_SWIPE object:imageView];

然后我实现了handleLeftSwipe 方法。当收到通知时将调用此函数。

现在,在我的子视图中,当收到滑动手势时,我会发送通知:

// Note that NOTIFICATION_LEFT_SWIPE is the same one used in the view controller
// I put this in a global header I use.  This is how you keep track of notifications.
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@NOTIFICATION_LEFT_SWIPE object:self]; 

对于右侧滑动也类似。

I have a UIViewController whose view has a custom subview.

This custom subview needs to track touch events and report swipe gestures.

Currently I put touchesBegan, touchesMoved, touchesEnded and touchesCancelled in the subview class. With some extra logic I am able to get swipe gestures and call my handleRightSwipe and handleLeftSwipe methods. So now when I swipe within the subview it calls its local swipe handling methods. This all works fine.

But what I really need is for the handleRightSwipe and handleLeftSwipe methods to be in the view controller. I could leave them in the subview class but then I'd have to bring in all the logic and data as well and that kind of breaks the MVC idea.

So my question is is there a clean way to handle this? Essentially I want to keep my touch event methods in the subview so that they only trigger for that specific view. But I also want the view controller to be informed when these touch events (or in this case swipe gestures) occur.

Any ideas?

Thanks.

UPDATE:

Using Henrik's suggestion, here's a quick sample of what I did (to save you the reading):

I set my view controller as an observer of notifications (early on).

// NOTIFICATION_LEFT_SWIPE is defined as some unique string elsewhere.
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

// Note that imageView is the instance of my subview that is calling the notification.
// You can set this to nil if you don't want it to be specific.
[nc addObserver:self selector:@selector(handleLeftSwipe) name:@NOTIFICATION_LEFT_SWIPE object:imageView];

Then I implement the handleLeftSwipe method. This will be called when a notification is received.

Now in my subview I send a notification when a swipe gesture is received:

// Note that NOTIFICATION_LEFT_SWIPE is the same one used in the view controller
// I put this in a global header I use.  This is how you keep track of notifications.
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@NOTIFICATION_LEFT_SWIPE object:self]; 

And similarly for the right swipe.

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

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

发布评论

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

评论(2

最丧也最甜 2024-09-13 02:35:31

您可以使用通知中心向不同的(观察)对象发送通知。

You could use the Notification Center to send notifications to different (observing) objects.

好倦 2024-09-13 02:35:31

您还可以使用手势识别器系统,该系统在 OS 3.2 及更高版本中可用;手势识别器可让您将触摸处理与视图类解耦,并且其设计几乎正是您在这里所做的事情。在本例中,您需要创建一个 UIPanGestureRecognizer 实例,将控制器添加为识别器的目标(使用常用的 -addTarget:action: 语法),然后添加使用 -addGestureRecognizer: 将识别器添加到您的视图中。然后,当用户在视图上滑动时,您的控制器将收到一系列调用的操作消息,并且可以通过调用识别器的 -translationInView: 方法来检查移动。

You could also use the gesture-recognizer system, which is available in OS 3.2 and later; gesture recognizers let you decouple touch handling from your view classes, and are designed for pretty much exactly the kind of thing you're doing here. In this case, you'd create an instance of UIPanGestureRecognizer, add your controller as a target of the recognizer (using the usual -addTarget:action: syntax), then add the recognizer to your view with -addGestureRecognizer:. Your controller, then, would get a series of action messages called on it as the user swiped across the view, and could check on the movement by calling the recognizer's -translationInView: method.

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