在touchesMoved方法中检测[UITouch view]

发布于 2024-10-15 23:43:16 字数 193 浏览 5 评论 0原文

我希望在应用程序中从一个子视图拖动到另一个子视图(并用一条线连接它们),因此我需要跟踪用户触摸的当前视图。

我认为我可以通过简单地在 TouchMoved 方法中调用 [UITouch view] 来实现此目的,但快速查看文档后我发现 [UITouch view] 只返回发生初始触摸的视图。

有谁知道我如何在拖动过程中检测到被触摸的视图?

I wish to drag from one subview to another within my application (and connect them with a line), and so I need to keep track of the current view being touched by the user.

I thought that I could achieve this by simply calling [UITouch view] in the touchesMoved method, but after a quick look at the docs I've found out that [UITouch view] only returns the view in which the initial touch occurred.

Does anyone know how I can detect the view being touched while the drag is in progress?

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

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

发布评论

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

评论(3

っ〆星空下的拥抱 2024-10-22 23:43:16

还有我的方式:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([self pointInside:[touch locationInView:self] withEvent:event]) {
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
    } else {
        [self sendActionsForControlEvents:UIControlEventTouchUpOutside];
    }
}

And my way:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([self pointInside:[touch locationInView:self] withEvent:event]) {
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
    } else {
        [self sendActionsForControlEvents:UIControlEventTouchUpOutside];
    }
}
囚我心虐我身 2024-10-22 23:43:16

经过更多研究后,我找到了答案。

最初,我是这样检查视图的:

if([[touch view] isKindOfClass:[MyView* class]])
{
   //hurray.
}

但是,正如我的问题中所解释的,[触摸视图]返回原始触摸发生的视图。这可以通过将上面的代码替换为以下代码来解决:

if([[self hitTest:[touch locationInView:self] withEvent:event] isKindOfClass:[MyView class]])
{
    //hurrraaay! :)
}

希望这有帮助

After a bit more research I found the answer.

Originally, I was checking for the view like so:

if([[touch view] isKindOfClass:[MyView* class]])
{
   //hurray.
}

But, as explained in my question, the [touch view] returns the view in which the original touch occurred. This can be solved by replacing the code above with the following:

if([[self hitTest:[touch locationInView:self] withEvent:event] isKindOfClass:[MyView class]])
{
    //hurrraaay! :)
}

Hope this helps

凡尘雨 2024-10-22 23:43:16

UIView 是 UIResponder 的子类。因此,您可以重写继承自 UIView 的自定义类中的触摸结束/开始方法。您应该向该类添加委托并为其定义协议。在方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

或任何交互中,您只需向对象的委托发送适当的消息并传递该对象即可。例如

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   [delegate touchesBeganInView: self withEvent: event];
}

UIView is subclass of UIResponder. So you can override touches ended/ began methods in your custom class, that inherits from UIView. Than you should add delegate to that class and define protocol for it. In the methods

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

or whatever interactions you need just send appropriate message to object's delegate also passing this object. E.g.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   [delegate touchesBeganInView: self withEvent: event];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文