简体   繁体   English

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

[英]UIView, how to determine when touches entered the view

It appears that all the touch methods of a UIView are only called if the touches began within the bounds of that view. 似乎只有在触摸开始于该视图的边界内时才会调用UIView的所有触摸方法。 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). 如果重要,我的具体应用是拖动MKPinAnnotationView(使用内置的4.0拖动)。 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). 如果用户将一个引脚拖动到另一个视图(这恰好是一个AnnotationView,但它可能是任何东西),我想要发生一些事情。 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. 并且没有方法没有被拖动的UIView似乎被调用,除非我从视图中触摸开始。

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. 因为superview是MKMapView,所以很难只使用touchesMoved事件并检查用户是否在正确的位置。 Thanks! 谢谢!

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; 事实证明你可以继承UIGestureRecognizer; and have it handle all the touches for the view that it has been added to (including an MKMapView). 并让它处理已添加到的视图的所有触摸(包括MKMapView)。 This allows all the normal MKMapView interactions to still behave without any problem; 这允许所有正常的MKMapView交互仍然没有任何问题; but also alerts me of the touches. 但也提醒我接触。 In touchesMoved, I just check the location of the touch; 在touchesMoved中,我只是检查触摸的位置; 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. 这似乎是在用户拖动MKAnnotation时拦截touchesMoved的唯一方法。

You sure can: 你肯定可以:

(HitstateView.h) (HitstateView.h)

#import <UIKit/UIKit.h>


@interface HitstateView : UIView {
    id overrideObject;
}

@property (nonatomic, retain) id overrideObject;

@end

(HitstateView.m) (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. 将overideObject设置为您希望触摸的视图。 IIRC it ought to be a subview of the HitstateView. IIRC它应该是HitstateView的子视图。

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. 每个视图都继承UIResponder因此每个视图都可以触摸开始/移动/结束 - 我不认为在视图外部开始触摸意味着当触摸在视图上移动时视图不会发生任何事件。 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. 如果你想获得一些信息被拖到你的MKMapView上的通知,你应该创建一个处理触摸的子类,然后将事件传递给super ,允许层次结构做任何触摸需要做的事情。 You don't need to capture or modify the event just observe it. 您只需观察它就不需要捕获或修改事件。

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 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. 你的特殊情况听起来很奇特,所以你可能不得不玩一些技巧,比如父视图的框架大到足以包含两个有问题的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM