UILongPressGestureRecognizer 执行两次的问题

I am detecting if the user has pressed down for 2 seconds:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                             initWithTarget:self 
                                             action:@selector(handleLongPress:)];
        longPress.minimumPressDuration = 2.0;
        [self addGestureRecognizer:longPress];
        [longPress release];

This is how I handle the long press:

-(void)handleLongPress:(UILongPressGestureRecognizer*)recognizer{
    NSLog(@"double oo");
}

The text "double oo" gets printed twice when I press down for longer than 2 seconds. Why is this? How can I fix?

share | improve this question

 
feedback

4 Answers

up vote 51 down vote accepted

UILongPressGestureRecognizer is a continuous event recognizer. You have to look at the state to see if this is the start, middle or end of the event and act accordingly. i.e. you can through away all events after the start, or only look at movement as you need. From the Class Reference:

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.

share | improve this answer
 
2  
The next answer down shows how to do it, but I gave this answer the +1 because of its detailed explaination and link to documentation. –  Matt Connolly  Mar 23 '11 at 1:13
2  
Could be more helpful with a code example, rather than linking just to the documentation. I have posted my code snippet below. check the UIGestureRecognizerStateBegan state. –  Paul Solt  Aug 18 '11 at 20:58
feedback

To check the state of the UILongPressGestureRecognizer just add an if statement on the selector method:

- (void)handleLongPress:(UILongPressGestureRecognizer*)sender { 
    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Long press Ended");
    }
    else {
        NSLog(@"Long press detected.");
    }
}
share | improve this answer
 
8  
You don't want that if/else block, since there are more states than Ended. "Long press detected" will print multiple times when the state changes. Check the UIGestureRecognizerStateBegan state instead. –  Paul Solt  Aug 18 '11 at 20:59
 
Exactly. Just got bitten by the same if/else code. –  Stephen Eilert  Mar 14 at 0:48
 
Someone should really edit that answer to fit with what the top comment says. As it stands the code provided does not work. –  Deco  Sep 11 at 22:40
feedback

You need to check the correct state, since there are different behaviors for each state. Most likely you're going to need the UIGestureRecognizerStateBegan state with the UILongPressGestureRecognizer.

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                             initWithTarget:self 
                                             action:@selector(handleLongPress:)];
        longPress.minimumPressDuration = 1.0;
        [myView addGestureRecognizer:longPress];
        [longPress release];

...

- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
    if(UIGestureRecognizerStateBegan == gesture.state) {
        // Called on start of gesture, do work here
    }

    if(UIGestureRecognizerStateChanged == gesture.state) {
        // Do repeated work here (repeats continuously) while finger is down
    }

    if(UIGestureRecognizerStateEnded == gesture.state) {
        // Do end work here when finger is lifted
    }
}
share | improve this answer
 
Seems like you have to move your fingers to get the state to change; is that correct? –  magic-c0d3r  Jan 18 at 4:46
 
It might trigger StateChanged when moving fingers around, that sounds similar to what I was doing in my test code. –  Paul Solt  Jan 20 at 4:12
 
UIGestureRecognizerStateBegan seems to only be called once which is perfect for my situation trying to show a dialog on detecting a long press on a button. The other states get called multiple times. Thanks! –  Damian Mar 9 at 13:29
feedback
-(IBAction)longPressHandle:(id)sender{
    if (accessedLongPressHandlerCount == 0) {
        accessedLongPressHandlerCount = 1;
        //do something

    } else if (accessedLongPressHandlerCount == 1) {
        accessedLongPressHandlerCount = 0; 
        //this is whats called after the second time it gets to this method
    } else {
        NSLog(@"Accessed Long Pressed some how made it here %@ times", accessedLongPressHandlerCount);
    }
}

Is what worked for me.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值