在我的应用程序中,我将多个小视图连接在一起以形成一个大画布。我分别为每个视图正确获取了触摸开始/移动/结束事件。我现在想要的是,如果我触摸view1并将手指从view1拖出并进入view2的区域而不抬起手指,我希望view2能够以某种方式得到我现在在该视图中的通知,即view2 。谢谢。

最佳答案

我能够使用touchesMoved方法做到这一点。这是代码:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
CGPoint nowPoint = [touches.anyObject locationInView:self.view];
NSLog(@"%f, %f", nowPoint.x, nowPoint.y);

NSArray *viewsToCheck = [self.view subviews];
for (UIView *v in viewsToCheck)
{
    if ([v isKindOfClass:[CharacterTile class]])
    {
        if (CGRectContainsPoint(v.frame, nowPoint))
        {
            CharacterTile *ctTemp = (CharacterTile *)v;
            //perform your work with the subview.
        }
    }
}
}


其中CharacterTile是在self.view上添加的子视图。
CGRectContainsPoint指示用户触摸的点是否在视图内。

09-26 21:23