Combine longpress gesture and drag gesture together

后端 未结 5 752
执笔经年
执笔经年 2020-12-07 20:37

I\'m moving my views by

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveRight:)];
[panRecogn         


        
5条回答
  •  囚心锁ツ
    2020-12-07 21:15

    UILongPressGestureRecognizer already does what you want for you. Take a look at the UIGestureRecognizerState property. From the documentation:

    Long-press gestures are continuous. The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted.

    So essentially after your UILongPressGestureRecognizerselector is called you listen to UIGestureRecognizerStateBegan, UIGestureRecognizerStateChanged, UIGestureRecognizerStateEnded. Keep changing your views frame during UIGestureRecognizerStateChanged.

    - (void)moveRight:(UILongPressGestureRecognizer *)gesture
    {
        if(gesture.state == UIGestureRecognizerStateBegan)
        {
            //if needed do some initial setup or init of views here
        }
        else if(gesture.state == UIGestureRecognizerStateChanged)
        {
            //move your views here.
            [yourView setFrame:];
        }
        else if(gesture.state == UIGestureRecognizerStateEnded)
        {
            //else do cleanup
        }
    }
    

提交回复
热议问题