在iOS SDK中通过手指滑动获取当前视图

时间:2013-05-15 18:28:16

标签: ios touch touch-event

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

1 个答案:

答案 0 :(得分:1)

我能够使用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告诉用户触摸的点是否在视图内。

相关问题