iPhone hitTest 旋转后损坏

发布于 2024-08-27 20:13:22 字数 619 浏览 4 评论 0原文

我有一个包含许多 CALayer 子类的 UIView。我使用以下代码来检测触摸事件对应的层:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
NSLog(@"%@,%@",NSStringFromCGPoint(point),[self.layer hitTest:point].name);


}

在设备旋转之前,这一切正常。当设备旋转时,所有当前图层都会从超级图层中删除,并创建新的 CALayers 以适应新的方向。新图层已正确插入并可以在正确的方向查看。

旋转后,hitTest 方法始终为该层返回 null。我注意到,当旋转 180 度时,返回的图层是旋转之前所在位置的图层,即触摸左上图层会在旋转 180 度时给出右下图层。命中测试的坐标按预期打印,(0,0) 位于左上角。我每次旋转都会重新绘制图层,但由于某种原因,它们似乎被映射为“正确”的向上方向,主页按钮位于底部。在处理旋转后我是否错过了函数调用或其他内容?

干杯, 亚当

I have a UIView that contains a number of CALayer subclasses. I am using the following code to detect which layer a touch event corresponds to:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
NSLog(@"%@,%@",NSStringFromCGPoint(point),[self.layer hitTest:point].name);


}

This works fine until the device is rotated. When the device is rotated all current layers are removed from the superlayer, and new CALayers are created to fit the new orientation. The new layers are correctly inserted and viewable in the correct orientation.

After the rotation the hitTest method consistently returns null for the layer. I have noticed that when rotated 180 degrees, the returned layer is what was in that location before the rotation, i.e. touching the top left layer gives the layer in the bottom right when rotated 180 degrees. The coordinates of the hit test are printed as expected with (0,0) being in the top left. I redraw the layers with every rotation, but for some reason they seem to be mapped to being the "correct" way up, with the home button at the bottom. Am I missing a function call or something after handling the rotation?

Cheers,
Adam

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

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

发布评论

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

评论(1

屌丝范 2024-09-03 20:13:22

好的,我发现以下代码可以在所有方向上工作,没有任何问题(在我的情况下,没有重叠视图,因此这适合我):

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

    CGPoint point = [t locationInView:self];

    for(CALayer *layer in self.layer.sublayers) {

        CGPoint convertedPoint = [self.layer convertPoint:point
                                                  toLayer:layer];

        if([layer containsPoint:convertedPointPoint]) {
            NSLog(@"%@",layer.name);
        }
    }
}

虽然此手动点转换工作正常,但问题仍然是为什么原来的方法调用没有。有人可以启发我吗?

亚当

Okay, I've found that the following code works in all orientations without any issues (In my case there are no overlapping views so this is appropriate for me):

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

    CGPoint point = [t locationInView:self];

    for(CALayer *layer in self.layer.sublayers) {

        CGPoint convertedPoint = [self.layer convertPoint:point
                                                  toLayer:layer];

        if([layer containsPoint:convertedPointPoint]) {
            NSLog(@"%@",layer.name);
        }
    }
}

While this manual point conversion works correctly, the question still remains as to why the original method call did not. Can anybody enlighten me?

Adam

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