views:

21

answers:

1

I'd like to reproduce this behavior in my iPad application. I have a subView that contains four custom buttons.

  • The view has an alpha value of 0.0
  • I have another custom button outside of the view descripted above that is always visible.
  • When the user touches the visible button the view appear animating its alpha to 1.0 showing the other 4 buttons.
  • Now I'like to start a timer that fires the view fadeOut after 2 seconds
  • When and if the user interact (say touchDown or whatever) with the buttons the timers need to be reset.

In other words the view may disappear only when nobody touches a button inside It.

Can you help me with this.

I've managed to learn the basics of UIView animation but I don't know how to queue them. My iPad has iOS 3.2.2. installed. Sorry for the bad explanation but this is my first iPad application and my first obj-c project.

A: 

You'd keep an NSTimer instance variable for that. As soon as your view is completely visible, which you can notice by e.g. implementing the fade in animation's delegate, you initialize it like this:

_fadeTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(fade:) userInfo:nil repeats:NO];

Make sure _fadeTimer is an instance variable, you need to be able to reset it. Then implement the fade out method:

- (void)fade:(NSTimer *)aTimer {
    // Forget about timer!
    _fadeTimer = nil;

    [UIView beginAnimations:nil context:NULL];
    // fade here
    [UIView commitAnimations];
}

Upon every user interaction you just call a method that delays the fade. To do this, delete and re-create the timer. Or change it's fire date:

- (void)delayFade {
    [_fadeTimer setFireDate: [NSDate dateWithTimeIntervalSinceNow: 2.0]];
}

PS: There is no need to explicitly retain the timer. It's retained by the runloop until it fires. After the callback, it will be released anyways. Just make sure you always reset the variable to nil, otherwise your app may crash on an invalid access. If you need to delete the time beofre it fired, call the invalidate method.

Max Seelemann
Thanks a lot simple but effective solution! It worked! I wonder how It will change when blocks syntax arrives to the iPad.
microspino