如何在Objective-C中实现滑动和保持手势识别器?

时间:2017-01-03 10:12:39

标签: ios objective-c uigesturerecognizer uiswipegesturerecognizer uilongpressgesturerecogni

我正在开发一个应用程序,它的工作方式类似于Game Console,机顶盒等不同设备的遥控器。

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

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

  1. 1根手指点击使用UITapGestureRecognizer

  2. 2指使用UITapGestureRecognizer

  3. 点击
  4. 使用UISwipeGestureRecognizer进行1次手指轻扫

  5. 使用UISwipeGestureRecognizer进行2次手指滑动

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

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

    我的代码段:

    -(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;
        }
    }
    

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

    提前致谢!

0 个答案:

没有答案