iPhone iOS how to add a UILongPressGestureRecognizer and UITapGestureRecognizer to the same control and prevent conflict?

前端 未结 5 506
伪装坚强ぢ
伪装坚强ぢ 2021-02-01 19:25

I\'m building an iPhone app that would let the user rearrange some of the UI elements on the screen.

How can I add a tap gesture recognizer and a long press gesture rec

5条回答
  •  北恋
    北恋 (楼主)
    2021-02-01 19:48

    As an alternative approach, don't have two separate recognisers - just use the LongPress recogniser for both events:

    Configure as follows:

    UILongPressGestureRecognizer* longPress = [ [ UILongPressGestureRecognizer alloc ] initWithTarget:self.nextResponder action:@selector(longPressEvent:)];
        categoryPanelDrag.minimumPressDuration = 0.0;
    

    Then handle as follows:

    - (BOOL)longPressEvent:(UILongPressGestureRecognizer *)gesture {
    
        // _dragStarted is a class-level BOOL
    
        if(UIGestureRecognizerStateBegan == gesture.state) {
            _dragStarted = NO;
        }
    
        if(UIGestureRecognizerStateChanged == gesture.state) {
            _dragStarted = YES;
            // Do dragging stuff here
        }
    
        if(UIGestureRecognizerStateEnded == gesture.state) {
    
            if (_dragStarted == NO)
            {
                // Do tap stuff here
    
            }
            else
            {
                // Do drag ended stuff here
            }
        }
    
        return YES;
    
    }
    

提交回复
热议问题