UIView,如何确定触摸何时进入视图

发布于 2024-10-03 01:03:29 字数 326 浏览 8 评论 0原文

看起来,只有当触摸在该视图的边界内开始时,才会调用 UIView 的所有触摸方法。有没有办法让视图响应在视图之外触摸,然后将手指拖到视图中的用户?

如果重要的话,我的具体应用程序是拖动 MKPinAnnotationView (使用内置 4.0 拖动)。我希望如果用户将图钉拖到另一个视图上(这也恰好是 AnnotationView,但它可以是任何东西),就会发生一些事情。在我松开引脚之前,不会调用任何拖动方法;除非我从视图内触摸开始,否则似乎不会调用被拖动的 UIView 的方法。

因为超级视图是 MKMapView,所以很难仅使用它的 TouchMoved 事件来检查用户是否位于正确的位置。谢谢!

It appears that all the touch methods of a UIView are only called if the touches began within the bounds of that view. Is there a way to have a view respond to a user who has touched outside the view, but then dragged his fingers into the view?

In case it matters, my specific application is for dragging a MKPinAnnotationView (using built-in 4.0 dragging). I want something to happen if the user drags a pin onto another view (which happens to be an AnnotationView as well, but it could be anything). No method for dragging is called until I let go of the pin; and no method no the UIView that's being dragged to seems to be called unless I started by touching from within the view.

Because the superview is a MKMapView, it is difficult to just use the touchesMoved event of that and check if the user is in the right location or not. Thanks!

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

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

发布评论

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

评论(4

赠我空喜 2024-10-10 01:03:29

因此,在玩了一段时间后,我发现给出的答案 这里实际上给了我我需要的东西,尽管提出的问题不同。

事实证明,您可以对 UIGestureRecognizer 进行子类化;并让它处理已添加到的视图(包括 MKMapView)的所有触摸。这允许所有正常的 MKMapView 交互仍然可以正常运行,没有任何问题;但也提醒我触摸。在touchesMoved中,我只是检查触摸的位置;看看它是否在我的其他观点的范围内。

从我尝试过的一切来看;这似乎是用户拖动 MKAnnotation 时拦截 TouchMoved 的唯一方法。

So after playing around with it for a while, I found that the answer given here actually gave me what I needed, even though the question being asked was different.

It turns out you can subclass UIGestureRecognizer; and have it handle all the touches for the view that it has been added to (including an MKMapView). This allows all the normal MKMapView interactions to still behave without any problem; but also alerts me of the touches. In touchesMoved, I just check the location of the touch; and see if it is within the bounds of my other view.

From everything I tried; this seems to be the only way to intercept touchesMoved while the user is dragging an MKAnnotation.

半步萧音过轻尘 2024-10-10 01:03:29

您当然可以:

(HitstateView.h)

#import <UIKit/UIKit.h>


@interface HitstateView : UIView {
    id overrideObject;
}

@property (nonatomic, retain) id overrideObject;

@end

(HitstateView.m)

#import "HitstateView.h"


@implementation HitstateView

@synthesize overrideObject;

- (void)dealloc {
    self.overrideObject = nil;

    [super dealloc];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *hitView = [super hitTest:point withEvent:event];
    if (hitView == self) {
        return overrideObject;
    }
    return hitView;
}

@end

将此视图设置为触摸区域的大小。将 overideObject 设置为您想要触摸的视图。 IIRC 它应该是 HitstateView 的子视图。

You sure can:

(HitstateView.h)

#import <UIKit/UIKit.h>


@interface HitstateView : UIView {
    id overrideObject;
}

@property (nonatomic, retain) id overrideObject;

@end

(HitstateView.m)

#import "HitstateView.h"


@implementation HitstateView

@synthesize overrideObject;

- (void)dealloc {
    self.overrideObject = nil;

    [super dealloc];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *hitView = [super hitTest:point withEvent:event];
    if (hitView == self) {
        return overrideObject;
    }
    return hitView;
}

@end

Make this view the size of your touch area. Set the overideObject to the view you want the touches to go. IIRC it ought to be a subview of the HitstateView.

ヅ她的身影、若隐若现 2024-10-10 01:03:29

每个视图都继承UIResponder,因此每个视图都会得到touchesBegan/Moved/Ended - 我不认为在视图之外启动触摸意味着当触摸在视图上移动时视图不会收到任何事件。如果您想收到某个东西已被拖到 MKMapView 上的通知,您应该创建一个处理触摸的子类,然后将事件传递给 super,从而允许层次结构执行其需要执行的任何操作触摸。您不需要捕获或修改事件,只需观察它即可。

Every view inherits UIResponder so every view gets touchesBegan/Moved/Ended - I do not think starting the touch outside the view means the view gets no event when the touch moves over the view. If you want to get a notification that something has been dragged onto your MKMapView you should make a subclass that handles the touch but then passes the event to super, allowing the hierarchy to do whatever it needs to do with the touch. You don't need to capture or modify the event just observe it.

墨小沫ゞ 2024-10-10 01:03:29

这取决于你的观点是如何建立的。一般来说,利用响应者链是最好的方法。它允许您玩一些花招,尽管它可能过于具体而无法满足您的特定需求。

您还可以通过覆盖命中测试来玩弄转发事件:

http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MultitouchEvents/MultitouchEvents.html%23// apple_ref/doc/uid/TP40009541-CH3-SW3

您的特殊情况听起来很奇特,因此您可能需要玩一些技巧,例如拥有一个框架足够大以包含相关视图的父视图。

It depends on how your views are set up. Generally leveraging the responder chain is the best way to go. It allows you to play tricks, though it may be too specific to address your particular needs.

You can also play tricks with forward events by override hit testing:

http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MultitouchEvents/MultitouchEvents.html%23//apple_ref/doc/uid/TP40009541-CH3-SW3

Your particular case sounds pretty exotic, so you may have to play tricks like having a parent view whose frame is large enough to contain both views in question.

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