即使矩形包含点,CGRectContainsPoint() 也不返回 TRUE?

发布于 2024-12-15 21:20:00 字数 598 浏览 5 评论 0原文

我有一个 UIView,里面有一个 UIImageView。当有人尝试拖动图像视图时,图像视图应跟随该人的手指。下面是 UIView 类的代码。

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {

CGPoint touchPoint = [touch locationInView:self];


    if ( CGRectContainsPoint(self.thumb.frame, touchPoint))

        self.thumb.center = CGPointMake(touchPoint.x, self.thumb.center.y);

return YES;

我通过将手指放在 imageView 上并尝试移动它来开始跟踪时,没有任何反应。从 NSLogging 中,我发现 if 语句永远不会被满足。

但是,当我开始在 imageView 外部跟踪并将手指拖动到 imageView 时,图像视图将跟随我的手指。

当我的手指开始放在图像视图上时,为什么 CGRectContainsPoint() 不起作用?

I have a UIView with a UIImageView inside it. When someone tries to drag the image view, the image view should follow the person's finger. Below is the code from the UIView's class.

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {

CGPoint touchPoint = [touch locationInView:self];


    if ( CGRectContainsPoint(self.thumb.frame, touchPoint))

        self.thumb.center = CGPointMake(touchPoint.x, self.thumb.center.y);

return YES;

}

When I start tracking by placing my finger on the imageView and trying to move it, nothing happens. From NSLogging, I figured out that the if statement was never satisfied.

However, when I start tracking outside of the imageView and drag my finger to the imageView, the image view will then follow my finger.

Why doesn't CGRectContainsPoint() work when my finger starts on the image view?

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

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

发布评论

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

评论(2

忆依然 2024-12-22 21:20:00
CGPoint touchPoint = [touch locationInView:self.view];

self 不是视图(通常),它是视图控制器。

请注意,还假设 self.view 是 self.thumb 的超级视图。如果没有,更一般地,您可以使用:

CGPoint touchPoint = [touch locationInView:[self.thumb superview]];
CGPoint touchPoint = [touch locationInView:self.view];

self is not a view (usually), it is a view controller.

Note that also assumes self.view is the superview of self.thumb. If not, more generally, you can use:

CGPoint touchPoint = [touch locationInView:[self.thumb superview]];
度的依靠╰つ 2024-12-22 21:20:00

触摸是如何定义的?
我一直在使用以下内容(在标准touchesBegan或touchesMoved中),尽管self是一个ViewController。

UITouch *touch = [[[event allTouches] allObjects] objectAtIndex:0];

How is touch being defined?
I have been using the following (within the standard touchesBegan or touchesMoved), though self is a ViewController.

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