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

前端 未结 5 593
伪装坚强ぢ
伪装坚强ぢ 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 20:04

    I did try moby and journeyman's approach but somehow they didn't fit my project well, so I solved like below,

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
        NSLog(@"%@ %ld",touch.description, touch.phase);
        [self performSelector:@selector(checkTouch:) withObject:touch afterDelay:0.5];
        return YES;
    }
    

    and

    - (void)checkTouch:(UITouch *)touch{
        NSLog(@"touch phase = %ld",touch.phase);
        if (touch.phase == UITouchPhaseStationary) {
            //still holding my hand and this means I wanted longPressTouch
        }
        if (touch.phase == UITouchPhaseEnded){
            //I released my finger so it's obviously tap 
        }
    }
    

    It could be simpler solution but of course it depends to project.

提交回复
热议问题