iPhone hitTest 旋转后损坏
我有一个包含许多 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我发现以下代码可以在所有方向上工作,没有任何问题(在我的情况下,没有重叠视图,因此这适合我):
虽然此手动点转换工作正常,但问题仍然是为什么原来的方法调用没有。有人可以启发我吗?
亚当
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):
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