UILongPressGestureRecognizer Bouncing

Discussion in 'iOS Programming' started by IDMah, Jul 11, 2012.

  1. IDMah macrumors 6502

    IDMah

    Joined:
    May 13, 2011
    #1
    Set up a UILongPressGestureRecognizer with:
    Code:
           // set up LongPress //
    	UILongPressGestureRecognizer *longPressRecognizer = 
    	[[UILongPressGestureRecognizer alloc]
    	 initWithTarget:self 
    	 action:@selector(longPressDetected:)];
        longPressRecognizer.minimumPressDuration = 3;
        longPressRecognizer.numberOfTouchesRequired = 1;
        [self.window addGestureRecognizer:longPressRecognizer];
        [longPressRecognizer release];
     
    
    and trigger an Alert with this.

    Code:
    -(void)longPressDetected:(UIGestureRecognizer *)recognizer {
    	NSLog(@"Long Press");
    	
    	UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Really reset?" message:@"Do you really want to reset this game?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] autorelease];
        // optional - add more buttons:
        [alert addButtonWithTitle:@"Yes"];
        [alert show];
    }
    
    but it seems to be triggering several Long Presses.. so showing more than one alert.. Can I clear the buffer some how .. or somehow do I Debounce this.. ???

    thanks
    Ian
     
  2. IDMah thread starter macrumors 6502

    IDMah

    Joined:
    May 13, 2011
    #2
    Dude might be hacky but add a flag that is triggered when Alert is displayed
    to stop more alerts. just remember to set it NO before..

    Code:
    -(void)longPressDetected:(UIGestureRecognizer *)recognizer {
    	NSLog(@"Long Press");
    	
    	UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Really reset?" message:@"Do you really want to reset this game?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] autorelease];
        // optional - add more buttons:
        [alert addButtonWithTitle:@"Yes"];
    	
    	if (alertShow==NO) {
    		[alert show];
    		}
    	alertShow=YES;
    }
    
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    
    	NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    	if([title isEqualToString:@"Cancel"])
        {
            NSLog(@"Cancel");
    		alertShow=NO;
    	}
        else if([title isEqualToString:@"Yes"])
        {
            NSLog(@"Yes");
    		alertShow=NO;
        }
        
    }
    
    Does anyone have a more elegant solution???
    thanks.
    Ian
     
  3. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #3
    Create an instance variable for an UIAlertView. Then check if it's visible via the UIAlertView.visible property. Not sure what you are trying to accomplish overall however are you sure adding a long gesture to window is ideal?

    Good luck.
     
  4. ArtOfWarfare macrumors G3

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Maybe your long press detector isn't tolerant enough of motion?

    I don't know what the behavior is if a touch strays too far... I'd think it would just cancel the gesture and the touch couldn't trigger the gesture again until the finger were lifted and pressed back down, but maybe not? Maybe if it strays outside it triggers a second long press?

    That's my only guess right now.
     
  5. IDMah thread starter macrumors 6502

    IDMah

    Joined:
    May 13, 2011
    #5
    tried but bounce

    I tried using UIAlertView.visible but still bouncing.
    The ugly hack seem to work..

    also played with the movement setting on LongPress. but nothing.
    I guess the correct way to to remove the longpress gesture once detected but that seem like more work than it's worth..

    Shrug.. so for now.. I'll make due with ugly code.
    but any other suggestions welcome.

    thanks
    Ian
     

Share This Page