Question

I'm working on an iOS app where I want to load one view if a UIButton is held for x seconds, another if it's held for x+y seconds, etc. I found a tutorial. The problem I'm running into is, how do I switch the length of the button press? The tutorial switched the number of taps.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSSet *allTouches = [event allTouches];

    switch ([allTouches count]) 
    {
        case 1: // Single touch
        { 
            // Get the first touch.
            UITouch *touch = [[allTouches allObjects] objectAtIndex:0];

            switch ([touch tapCount])
            {
                case 1: // Single Tap.
                {
                    // Start a timer
                    timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showAlertView:) userInfo:nil repeats:NO];
                    [timer retain];
                } 
                    break;
                case 2: // Double tap.
                    break;
            }
        } 
            break;
        case 2: // Double touch
        {
        } 
            break;
        default:
            break;
    }
}

Any suggestions?

Was it helpful?

Solution

I got my answer. I just started an NSTimer on a touch down event, and stopped in on touch up inside.

// TRP - On Touch Down event, start the timer
-(IBAction) startTimer
{
    self.time = 0;
    // TRP - Start a timer
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];

    [timer retain];     // TRP - Retain timer so it is not accidentally deallocated

}

// TRP - Method to update the timer display
-(void)updateTimer
{
    time++;
    NSLog(@"Seconds: %i ", time); 
    if (15 == self.time)
        [timer invalidate];
}

// TRP - On Touch Up Inside event, stop the timer & display results
-(IBAction) btn_MediaMeterResults
{
    [timer invalidate];
    NSLog(@"time: %i ", self.time);

    ResultsViewController *resultsView = [[ResultsViewController alloc] initWithNibName:@"ResultsViewController" bundle:nil];

    // TRP - The following line is passing the "time" variable from MediaMasterViewController to ResultsViewController
    resultsView.time = self.time;

    [self.view addSubview:resultsView.view];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top