UIView:如何检查触摸是否在开始的同一视图内结束
在 AcaniUsers
中,我在 UITableView
内创建了一个由 ThumbView : UIView
实例组成的网格。所有 thumbViews
的宽度均为 kThumbSize
。如何检测触摸是否在其开始的同一视图内结束?
In AcaniUsers
, I've created a grid of ThumbView : UIView
instances inside of a UITableView
. All thumbViews
have a width of kThumbSize
. How do I detect if touches ended inside the same view in which they began?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您使用的视图扩展中;
斯威夫特4:
In the view extension you use;
Swift 4:
以下方法有效,但我不确定这是否是最好的方法。我也这么认为。
由于所有
thumbViews
的宽度均为kThumbSize
,因此只需在touchesEnded
中检查locationInView
的 x 坐标UITouch
实例(假设self.multipleTouchEnabled = NO
)小于或等于kThumbSize
。这意味着触摸在thumbView
内结束。无需检查 y 坐标,因为如果触摸垂直移动,则包含thumbViews
的tableView
会滚动并且触摸会被取消。在
ThumbView : UIView
(其实例是UITableView
的子视图)中执行以下操作:要一次仅注册一个
thumbView
上的触摸,您可以还可能想在ThumbView
的init
实例方法中设置self.exclusiveTouch = YES;
。The following works, but I'm not sure if it's the best way to go about it. I think so though.
Since all
thumbViews
have a width ofkThumbSize
, just check intouchesEnded
that the x-coordinate of thelocationInView
of theUITouch
instance (assumingself.multipleTouchEnabled = NO
) is less than or equal tokThumbSize
. This means the touches ended inside thethumbView
. No need to check the y-coordinate because if the touches move vertically, thetableView
, which contains thethumbViews
, scrolls and the touches are cancelled.Do the following in
ThumbView : UIView
(whose instances are subviews of aUITableView
):To only register touches on one
thumbView
at a time, you also probably want to setself.exclusiveTouch = YES;
in theinit
instance method ofThumbView
.