0

我正在开发一个应用程序,它的工作原理类似于游戏控制台、机顶盒等不同设备的遥控器。

应用程序包含不同类型的控件,例如手势和按钮,以远程执行事件。

单个手势视图包含多个手势识别器,如下所示:

  1. 使用 UITapGestureRecognizer 的 1 个手指点击

  2. 使用 UITapGestureRecognizer 的 2 指点击

  3. 使用 UISwipeGestureRecognizer 1 手指轻扫

  4. 使用 UISwipeGestureRecognizer 2 手指滑动

我的要求是实现 1 个手指滑动手势识别器以及保持事件。因此,这将继续我的滑动操作,直到用户不会将其手指从视图中移开。

我在 UISwipeGestureRecognizer 之后尝试了 UILongPressGestureRecognizer 但这对我不起作用,因为它与滑动动作一起执行。我想要在滑动结束后这样做,但手指不会移除。

我的代码片段:

-(void) gestureView:(UIView*)viewGesture {
    UISwipeGestureRecognizer *swipeGestureSingleRight;
    UISwipeGestureRecognizer *swipeGestureSingleLeft;
    UISwipeGestureRecognizer *swipeGestureSingleUp;
    UISwipeGestureRecognizer *swipeGestureSingleDown;

    swipeGestureSingleUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureSingle_Handle:)];
    [swipeGestureSingleUp setDelegate:self];
    [swipeGestureSingleUp setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [swipeGestureSingleUp setNumberOfTouchesRequired:1];
    [viewGesture addGestureRecognizer:swipeGestureSingleUp];

    swipeGestureSingleDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureSingle_Handle:)];
    [swipeGestureSingleDown setDelegate:self];
    [swipeGestureSingleDown setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [swipeGestureSingleDown setNumberOfTouchesRequired:1];
    [viewGesture addGestureRecognizer:swipeGestureSingleDown];

    swipeGestureSingleRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureSingle_Handle:)];
    [swipeGestureSingleRight setDelegate:self];
    [swipeGestureSingleRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [swipeGestureSingleRight setNumberOfTouchesRequired:1];
    [viewGesture addGestureRecognizer:swipeGestureSingleRight];

    swipeGestureSingleLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureSingle_Handle:)];
    [swipeGestureSingleLeft setDelegate:self];
    [swipeGestureSingleLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [swipeGestureSingleLeft setNumberOfTouchesRequired:1];
    [viewGesture addGestureRecognizer:swipeGestureSingleLeft];

    longPressGestureOnSwipe = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture_Handle:)];
    longPressGestureOnSwipe.minimumPressDuration = 0.03;
    longPressGestureOnSwipe.delegate = self;
    [viewGesture addGestureRecognizer:longPressGestureOnSwipe];
    isSwipeEnd = false;
    [longPressGestureOnSwipe setEnabled:isSwipeEnd];
}

- (void)swipeGestureSingle_Handle:(UISwipeGestureRecognizer*)sender {
    if (sender.state == UIGestureRecognizerStateEnded) {
        singleTouchSwipeSenderDirection = sender.direction;
        isSwipeEnd = true;
        [longPressGestureOnSwipe setEnabled:isSwipeEnd];
    }
}

-(void)longPressGesture_Handle:(UILongPressGestureRecognizer*)sender {
    if (sender.state == UIGestureRecognizerStateEnded) {
        isSwipeEnd = false;
        [longPressGestureOnSwipe setEnabled:isSwipeEnd];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    if (isSwipeEnd) {
        [super touchesBegan:touches withEvent:event];
    } else {
        return;
    }
}

请建议我更正或解决我的要求。

提前致谢!

4

0 回答 0