Call another screen after a few seconds without user interaction

Asked

Viewed 194 times

0

I need to put in my app the following behavior:

After a few seconds without the user touching the screen, for example 10 seconds, the app presents another screen showing an image. And when the user touches the screen, the image screen disappears and goes back to the previous screen. And while the user is interacting with the app not appear anything.

The behavior will look like that of a screen saver.

  • 3

    There is nothing more irritating to a user than these screens that come out of nowhere unsolicited. A basic development principle says that you should not present anything that has not been requested by the user. If you want the user to perform an action after ten seconds, say it to him in the interface itself and not causing a popup screen to appear. The screen saver itself is a user option and not an imposition.

2 answers

1


You can also use the methods of UIResponder that your AppDelegate inherits.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    // Ao detectar o toque, reseta o timer
    [self resetarTimerDeInatividade];
}

- (void)resetarTimerDeInatividade {
    if (self.interacaoTimer) {
        // Caso o timer esteja ativo, invalidamos ele
        [self.interacaoTimer invalidate];
        self.interacaoTimer = nil;
    }

    NSInteger tempoMaximoSemInteracao = 10; // em segundos

    // Iniciamos o timer novamente
    self.interacaoTimer = [NSTimer scheduledTimerWithTimeInterval:tempoMaximoSemInteracao
                                                           target:self
                                                             selector:@selector(tempoInativoExcedido)
                                                         userInfo:nil
                                                          repeats:YES];
}

- (void)tempoInativoExcedido{
    // Aqui você pode disparar a exibição da sua tela temporária
}
  • I’ll test your code and I’ll bring you the answer if it worked or not!

1

A possible solution is to use a timer that is reset to each user interaction. When the time limit is reached, the timer will call the specified selector:

- (void)resetTimer {

    if (timer) {
        [timer invalidate];
        timer = nil;
    }

    timer = [NSTimer scheduledTimerWithTimeInterval:10
                                             target:self
                                           selector:@selector(idleTimerExceeded)
                                           userInfo:nil
                                            repeats:NO];
}

- (UIResponder *)nextResponder {

    [self resetTimer];
    return [super nextResponder];
}

You can use the method viewDidAppear to start the timer.

  • I will try your code and bring you the answer!! Thank you @Rafael Leão

  • viewDidAppear method is no longer available for iOS 7, or am I mistaken? Can’t do it in didViewLoad??

  • The method viewDidAppear: still available on iOS 7.

  • @Rafael Leão Your code works, but it shows the following error when the view I want enters the screen: Warning: Attempt to Dismiss from view controller <Uinavigationcontroller: 0x8c8dec0> while a Presentation or Dismiss is in Progress!

  • I am using modal transiction to make the screen I want to appear and dissmis to remove it when the screen is played.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.