如何在选择自定义UITableViewCell时获取触摸坐标? [英] How to get touch coordinates upon SELECTING a custom UITableViewCell?

查看:92
本文介绍了如何在选择自定义UITableViewCell时获取触摸坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我触摸(触摸)UITableViewCell我的ViewController的UITableViewDelegate方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 被调用。在这个时候,我需要得到触摸点的x坐标,以便我可以知道单元格的哪个部分被触摸(非附件项目)。我已经尝试使用通常的触摸方法,如TouchesDidEnd,但坐标总是返回x = 0.000 y = 0.000无论我在哪里触摸(在我的ViewController UITableView对象的自定义UITableViewCell中的位置)。我也尝试在自定义单元格类内实现触摸处理,而我COULD获得准确的坐标,我可以找到没有办法将这些坐标传递给我的ViewController类(它有UITableView)。

When I touch (Touch Up) a UITableViewCell my ViewController's UITableViewDelegate method - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath is called. I need to get the x-coordinate of the Touch-Point at this time as well so that I can know what portion of the Cell was touched (non-accessory items). I have tried using the usual touch methods such as TouchesDidEnd but the coordinates always return x=0.000 y=0.000 no matter where I touched (A location in a custom UITableViewCell in a UITableView object in my ViewController). I also tried implementing touch handling from within the Custom Cell class and while I COULD get accurate coordinates alas I could find no way to communicate those coordinates to my ViewController class (which has the UITableView).

问:当我触摸自定义UITableViewCell时,是否可以获得设备屏幕的x坐标? >

Q: Is there a good way I can get the x-coordinates of the device's screen when I touch a custom UITableViewCell?

推荐答案

在表视图上方放置一个透明的 UIView c> -touchesBegan:withEvent:等方法。在这些覆盖的方法中,获取 UITouch 对象并调用 -locationInView:,以获取 CGPoint 的值。

Put a transparent UIView atop your table view, override its -touchesBegan:withEvent: etc. methods. In those overridden methods, get the UITouch objects and call -locationInView: on them to get the CGPoint values.

这给出了触摸事件的坐标。

That gives you the coordinates of a touch event.

然后你只需将那些 UITouch 事件传递给基础表视图。表视图处理常见的将触摸传递到行等的业务。

Then you just pass on those UITouch events to the underlying table view. The table view handles its usual business of passing touches on to rows, etc.

这篇关于如何在选择自定义UITableViewCell时获取触摸坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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