Как я могу загрузить представление в зависимости от того, как долго я держу UIButton? - PullRequest
1 голос
/ 25 марта 2010

Я работаю над приложением для iOS, в котором я хочу загрузить одно представление, если кнопка UIB удерживается в течение x секунд, другое, если оно удерживается в течение x + y секунд, и т. Д. Я нашел учебник. Проблема, с которой я сталкиваюсь, состоит в том, как я могу изменить длину нажатия кнопки? В учебнике переключено количество нажатий.

-(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;
    }
}

Есть предложения?

Ответы [ 2 ]

1 голос
/ 08 апреля 2010

Я получил свой ответ. Я просто запустил NSTimer для события касания, и остановился при касании внутри.

// 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];
}
0 голосов
/ 25 марта 2010

Используйте класс UILongPressGestureRecognizer. Apple, это задокументировано в 3.2 - http://developer.apple.com/iphone/prerelease/library/documentation/General/Conceptual/iPadProgrammingGuide/GestureSupport/GestureSupport.html#//apple_ref/doc/uid/TP40009370-CH5-SW1

...