如何在传递给下一个响应者之前更改 UITouch 的位置 [英] How to change a UITouch's location before passing to the next responder

查看:22
本文介绍了如何在传递给下一个响应者之前更改 UITouch 的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父 UIView 和一个子 UIView,我想让触摸从子传递到父,并由两个视图处理.

I have a parent UIView and a child UIView, I wanna let a touch pass through from child to parent, and handled by the two views.

y--------------
| parent      |
|   x------   |
|   |child|   |
|   |_____|   |
|_____________|

所以在子视图中,我覆盖:

So in the child view, I override:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // child touch handle 
    // ...
    // parent touch handle
    [self.nextResponder touchesBegan:touches withEvent:event];
}

<删除>但是当我触摸 child 中的 `x` 时,它会转发到 parent(相对于 parent)中的 `y`.我想要一个传递效果(孩子中的`x`,传递到父母中的`x`),所以我需要在转发之前改变触摸的位置,对吗?我该怎么做?

谢谢@Fogmeister.就是这样.

but when I touch the `x` in child, it forwarded to `y` in parent (relative to the parent). I wanna a pass through effect (`x` in child, pass through to `x` in parent), so I need to change the touches' position before forwarding, right? How should I do this?

Thanks @Fogmeister. And that's it.

UITouch 现在可以传递给父级.并在父级的 touchesBegan 中,调用

The UITouch can now pass to parent. And in the parent's touchesBegan, call

[touch locationInView:self]

获取触摸位置.

推荐答案

TL:DR

不要进行任何转换,只需使用 locationInView: 方法.

Don't make any conversions, just use the locationInView: method.

长版

为此,您可以使用代码 locationInView: like this...

For this you can just use the code locationInView: like this...

UITouch *touch = [touches anyObject]; //assuming there is just one touch.

CGPoint touchPoint = [touch locationInView:someView];

这会将触摸的屏幕坐标转换为您传入的视图中的坐标.

This will convert the screen coordinate of the touch to a coordinate in the view you pass in.

即如果用户点击子视图中的点 (10, 10),然后您将其传递给下一个响应者,即父级.当你运行 [touch locationInView:parentView] 时,你会得到一个类似 (60, 60) 的点(从你的图表中粗略猜测).

i.e. if the user taps the point (10, 10) in the child view and then you pass it through to the next responder i.e. the parent. When you run [touch locationInView:parentView] you will get a point something like (60, 60) (taking rough guesses from your diagram).

locationInView 的 UITouch 文档

locationInView:返回接收器在给定视图坐标系中的当前位置.

locationInView: Returns the current location of the receiver in the coordinate system of the given view.

-(CGPoint)locationInView:(UIView *)view

-(CGPoint)locationInView:(UIView *)view

参数

查看

您希望触摸位于其坐标系中的视图对象.处理触摸的自定义视图可以指定 self 以在其自己的坐标系中获取触摸位置.传递 nil 以获取窗口坐标中的触摸位置.

The view object in whose coordinate system you want the touch located. A custom view that is handling the touch may specify self to get the touch location in its own coordinate system. Pass nil to get the touch location in the window’s coordinates.

返回值

在视图中指定接收器位置的点.

A point specifying the location of the receiver in view.

讨论

此方法返回 UITouch 对象在指定视图坐标系中的当前位置.由于触摸对象可能已从另一个视图转发到视图,此方法执行触摸位置到指定视图坐标系的任何必要转换.

This method returns the current location of a UITouch object in the coordinate system of the specified view. Because the touch object might have been forwarded to a view from another view, this method performs any necessary conversion of the touch location to the coordinate system of the specified view.

示例

您有一个名为 parentView 框架 (0, 0, 320, 480) 的视图,即整个屏幕.这有一个名为 childView 框架的子视图 (50, 50, 100, 100).

You have a view called parentView frame (0, 0, 320, 480) i.e. the whole screen. This has a subView called childView frame (50, 50, 100, 100).

在子视图中

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    CGPoint touchLocation = [touch locationInView:self];

    NSLog(@"Child touch point = (%f, %f).", touchLocation.x, touchLocation.y);

    [self.nextResponder touchesBegan:touches withEvent:event];
}

在父视图中

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    CGPoint touchLocation = [touch locationInView:self];

    NSLog(@"Parent touch point = (%f, %f).", touchLocation.x, touchLocation.y);
}

*现在...

用户在子视图的正中央按下屏幕.

The user presses the screen in the exact center of the child view.

程序的输出将是...

Child touch point = (50, 50). //i.e. this is the center of the child view relative to the **child view**.
Parent touch point = (150, 150). //i.e. this is the center of the child view relative to the **parent view**.

我根本没有做任何转换.方法 locationInView 为您完成所有这些.我认为你是想把它复杂化.

I have done no conversion at all. The method locationInView does all this for you. I think you are trying to over complicate it.

这篇关于如何在传递给下一个响应者之前更改 UITouch 的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆