2

我正在使用 UILongPressGestureRecognizer 来跟踪 UIView 内的触摸,这就是我的代码

-(void)tapGes:(UILongPressGestureRecognizer *)tapGesture
{
CGPoint location=[tapGesture locationInView:self.view];
UIView *view=tapGesture.view;
switch (tapGesture.state)
{
    case UIGestureRecognizerStateBegan:
        NSLog(@"enter");
        break;
    case UIGestureRecognizerStateEnded:
        NSLog(@"ended");
        break;
    case UIGestureRecognizerStateChanged:
        if (location.x>view.frame.size.width ||
            location.y>view.frame.size.height)
        {
            NSLog(@"out");
            return;
        }
        NSLog(@"change");
        break;
    default:
        break;
}
}

当状态 UIGestureRecognizerStateChanged 的​​触摸离开 UIView 时我想做什么我不再跟踪它,这样我就不会进入状态 UIGestureRecognizerStateEnded 如何实现这一点?

4

1 回答 1

2

您始终可以向层次结构添加额外的视图并将手势添加到该视图。这将使您能够跟踪手势位置并根据子视图的框架检查它。我的意思是,如果您当前的视图是 100x100 点,您可以为其创建一个透明的父视图,即 120x120 点。将手势添加到 120 点视图,然后将手势的位置与视图第一个子视图的框架进行比较。

-(void)tapGes:(UILongPressGestureRecognizer *)tapGesture
{
    CGPoint location=[tapGesture locationInView:tapGesture.view];
    UIView *view = tapGesture.view;

    switch (tapGesture.state)
    {
        case UIGestureRecognizerStateBegan:
            NSLog(@"enter");
            break;
        case UIGestureRecognizerStateEnded:
            NSLog(@"ended");
            break;
        case UIGestureRecognizerStateChanged:
            if (CGRectContainsPoint([view.subviews[0] frame], location)) {
                // location is within view
            }else{
                // location has exited view
            }
            NSLog(@"change");
            break;
        default:
            break;
    }
}
于 2013-09-01T14:55:09.567 回答