views:

166

answers:

2

In my ViewController I am using UIButton which triggers some action when touchedUpInside. Upto here no problem, but i also want to add another action for touchDown event. More precisely i dont have an idea how to add that action to which event, that will work the same as when App-Icon is pressed longer in springboard which causes springboard enter in editing mode.Actually what i want is : when this button is kept holding for ,say about 2 seconds it will pop-up an alertView without touch being heldUp (with finger is still on button).

I have tried with 2 NSdate difference, one of which is allocated when touchedDown and other when touchUpInside. Its working and popping alert,but only after touchedUpInside. I want it to show alert without touch being removed. Here is the code.

(IBAction)touchedDown:(id)sender {
momentTouchedDown = [[NSDate alloc] init];//class variable
NSLog(@"touched down");
}
- (IBAction)touchUpInside:(id)sender {
NSLog(@"touch lifted Up\n");    
NSDate *momentLifted = [[NSDate alloc] init];
double timeInterval = [momentLifted timeIntervalSinceDate:momentTouchedDown];
NSLog(@"time lifted = %@, time down = %@, time diff = %@", momentLifted, momentTouchedDown, [NSString stringWithFormat:@"%g",timeInterval]);
[momentLifted release];
   if(timeInterval > 2.0) {
            NSLog(@"AlertBox has been fired");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yes"
                                  message:@""//msg
                                  delegate:self
                                  cancelButtonTitle:@"Cancel"
                                  otherButtonTitles:@"OK"
                                  , nil];   
            [alert show];
            [alert release];
    }
}

Please provide me an insight..thanks for help in advance.

+1  A: 

Create an NSTimer when the user touches the control. The callback for the timer displays the alert. You'll also need to set some kind of flag that your touch up event handler can check so that it doesn't execute. You'll also want to invalidate the timer in the touch up event handler so that it doesn't fire when the user doesn't press and hold.

Edit: adding code sample to address comment

Add a BOOL variable called longPress to your class, and then do something like this:

// this is the method that gets called when the timer fires
- (void)longPressTimerFired:(NSTimer *)timer {
    longPress = YES;
    // your code to show the alert goes here
}

- (void)touchUpInside:(id)sender {
    if (longPress) {
        longPress = NO;
        return;
    }

    // your code to handle short taps goes here
}

This only handles a single touch event, so if your view controller supports multitouch, you'll need something more elaborate.

Alex
it will really be helpful if you please elaborate with an example code. I can create NStimer and schedule it to fire a method after 2 seconds after touching a button (toucheDown) and can invalidate it after touchedUp but how i'll know button is pressed holding. Please give me clue. Thank you once again.
KayKay
+1  A: 

The code-snippet by Alex will fire both short-tap and long-tap event if button is pressed long enough to exceed the time-interval scheduled in NSTimer. You can use your old code here and add it to.

-(IBAction)touchedDown:(id)sender {
momentTouchedDown = [[NSDate alloc] init];//class variable
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(longPressTimerFired:) userInfo:nil repeats:NO];
longPress = YES;
}
-(IBAction)touchUpInside:(id)sender {  
longPress = NO;
NSDate *momentLifted = [[NSDate alloc] init];
double timeInterval = [momentLifted timeIntervalSinceDate:momentTouchedDown];
[momentLifted release];
if(timeInterval < 0.5) {
  // your code to handle short taps goes here
  }
}
- (void)longPressTimerFired:(NSTimer *)timer {
  if(longPress) {
  // your code to show the alert goes here
  }
}
Kundan Pandit