设备方向更改后转换触摸坐标时出现问题

发布于 2024-12-02 09:08:47 字数 648 浏览 0 评论 0原文

好吧,场景是我有一个自定义类,它创建一个包含图像的视图并处理它自己的所有触摸事件。有时我想检查移动它是否会导致它与父视图上的另一个视图重叠。因此,在相关触摸事件之后,我在父视图上调用一个方法,将最后一个触摸坐标传递给它并返回一个 BOOL。当应用程序保持纵向时,这可以正常工作。

问题是旋转设备后,所有图像都会自动旋转,与父视图相比,传递的 CGPoint 现在处于旋转坐标系中,并且调用的 CGRectContainsPoint 方法不再正常工作。

在我的 willAnimateRotationToInterfaceOrientation 方法中,我正在调整每个自定义视图的位置以改进新方向的布局,例如

view.frame = CGRectMake((view.frame.origin.x*1.5),(view.frame.origin.y/1.5),view.frame.size.width,view.frame.size.height);

我读到调用 setBound 方法将重置坐标系:

[view setBounds:view.frame];

但这对我的情况没有影响。我还尝试修改“convertPoint:toView”,但它给出的值也没有多大意义。我的结论是,在坐标系旋转的情况下它不起作用?

Ok the scenario is that I have a custom class which creates a view containing an image and handles all its own touch events. Occasionally I want to check if moving it would cause it to overlap another view on a parent view. So after the relevent touch event I call a method on the parent view passing it the last touch cordinate and return a BOOL. This works fine while the application remains in portrait orientation.

Problem is after rotating the device and all the images are auto-rotated for me the CGPoint being passed is now in a rotated coordinate system compared with the parent view and the CGRectContainsPoint method being called no longer works properly.

In my willAnimateRotationToInterfaceOrientation method I am adjusting the position of each custom view to improve the layout for the new orientation e.g.

view.frame = CGRectMake((view.frame.origin.x*1.5),(view.frame.origin.y/1.5),view.frame.size.width,view.frame.size.height);

I read that calling the setBound method would then reset the coordinate system:

[view setBounds:view.frame];

but this isn't making a difference in my case. I also tried tinkering with 'convertPoint:toView' but the values it was giving weren't making much sense either. I concluded it wouldn't work in the case where the coordinate system had been rotated?

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

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

发布评论

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

评论(2

眼波传意 2024-12-09 09:08:47

当您的视图旋转(使用自动旋转机制)时,iOS 只需将 CGAffineTransform 应用于您的视图即可旋转其内容。

因此,您应该能够检索当前应用于 UIView 的 CGAffineTransform,然后使用 CGPointApplyAffineTransform 将此 CGAffineTransform 应用于 UITouch 的点(可能在反转 AffineTransform 之后,将逆变换应用于您的点。没有测试过这段代码,只是凭记忆写的)

UITouch* touch = [touches anyObject];
CGPoint pt = [touch locationInView:self];
CGAffineTransform inverseTransform = CGAffineTransformInvert(self.transform);
CGPoint transformedPoint = CGPointApplyAffineTransform(pt,inverseTransform);

When your view is rotated (using the autorotate mechanism), iOS simply apply a CGAffineTransform to your view, to rotate its content.

So you should be able to retrieve the current CGAffineTransform applied to your UIView, then apply this CGAffineTransform to the point of your UITouch using CGPointApplyAffineTransform (probably after inverting the AffineTransform, to apply the inverse transformation to your point. Haven't tested this code, only writing this from memory)

UITouch* touch = [touches anyObject];
CGPoint pt = [touch locationInView:self];
CGAffineTransform inverseTransform = CGAffineTransformInvert(self.transform);
CGPoint transformedPoint = CGPointApplyAffineTransform(pt,inverseTransform);
高速公鹿 2024-12-09 09:08:47

根 viewController 的视图始终表现得像纵向模式。您应该在根视图中插入一个新视图。根据苹果公司的说法,这个新视图将正确运行,给出正确的尺寸和坐标。

例如 ;

UIView *v = self;
while ([[v superview] superview] != NULL) {
    v = [v superview];
}

UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:v];

touchPoint 将是正确的。

The view of root viewController always acts like portrait mode. You should insert a new view inside of the root one. And this new view will acts correctly, will give right size and coordinates according to Apple says.

for example ;

UIView *v = self;
while ([[v superview] superview] != NULL) {
    v = [v superview];
}

UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:v];

touchPoint will be the correct one.

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