UIView:如何检查触摸是否在开始的同一视图内结束

发布于 2024-11-03 03:11:12 字数 189 浏览 7 评论 0原文

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 技术交流群。

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

发布评论

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

评论(2

审判长 2024-11-10 03:11:12

在您使用的视图扩展中;

斯威夫特4:

open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
    guard let touchPoint = touches.first?.location(in: self) else { return }
    guard self.bounds.contains(touchPoint) else { return }
    // Do items for successful touch inside the view
}

In the view extension you use;

Swift 4:

open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
    guard let touchPoint = touches.first?.location(in: self) else { return }
    guard self.bounds.contains(touchPoint) else { return }
    // Do items for successful touch inside the view
}
清风不识月 2024-11-10 03:11:12

以下方法有效,但我不确定这是否是最好的方法。我也这么认为。

由于所有 thumbViews 的宽度均为 kThumbSize,因此只需在 touchesEnded 中检查 locationInView 的 x 坐标UITouch 实例(假设 self.multipleTouchEnabled = NO)小于或等于 kThumbSize。这意味着触摸在 thumbView 内结束。无需检查 y 坐标,因为如果触摸垂直移动,则包含 thumbViewstableView 会滚​​动并且触摸会被取消。

ThumbView : UIView(其实例是 UITableView 的子视图)中执行以下操作:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touchesEnded %@", touches);
    CGPoint touchPoint = [[touches anyObject] /* only one */ locationInView:self];
    if (touchPoint.x >= 0.0f && touchPoint.x <= kThumbSize) {
        [(ThumbsTableViewCell *)self.superview.superview thumbTouched:self];
    }
}

要一次仅注册一个 thumbView 上的触摸,您可以还可能想在 ThumbViewinit 实例方法中设置 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 of kThumbSize, just check in touchesEnded that the x-coordinate of the locationInView of the UITouch instance (assuming self.multipleTouchEnabled = NO) is less than or equal to kThumbSize. This means the touches ended inside the thumbView. No need to check the y-coordinate because if the touches move vertically, the tableView, which contains the thumbViews, scrolls and the touches are cancelled.

Do the following in ThumbView : UIView (whose instances are subviews of a UITableView):

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touchesEnded %@", touches);
    CGPoint touchPoint = [[touches anyObject] /* only one */ locationInView:self];
    if (touchPoint.x >= 0.0f && touchPoint.x <= kThumbSize) {
        [(ThumbsTableViewCell *)self.superview.superview thumbTouched:self];
    }
}

To only register touches on one thumbView at a time, you also probably want to set self.exclusiveTouch = YES; in the init instance method of ThumbView.

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