2

我有一个 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() 不起作用?

4

2 回答 2

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

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

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

CGPoint touchPoint = [touch locationInView:[self.thumb superview]];
于 2011-11-15T05:55:21.650 回答
0

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

UITouch *touch = [[[event allTouches] allObjects] objectAtIndex:0];
于 2011-11-23T23:43:54.983 回答