Mimicking UIAlertView’s animated transition

For various reasons I ended up writing a UIView that mostly mimics the behavior of UIAlertView. UIAlertView appears on screen with pleasant animated fade in and rubber-band-like bounce of its dimensions. After struggling to reproduce this through trial and error, I gave up and just recorded video of a UIAlertView appearing. I then measured its dimensions over time. It goes through several steps, so to save anyone future effort doing the same thing, here are the phases it goes through (of course I could be a bit off, these are all hand measurements:

Before the animation starts, the view transform is 0.6, 0.6
Then a duration 0.2 animation takes us up to 1.1, 1.1
then a duration 1/15. animation down to 0.9, 0.9
then a duration 1/7.5 animation back to identity.

Here it all is wrapped in a simple method:

float pulsesteps[3] = { 0.2, 1/15., 1/7.5 };
- (void) pulse {
    self.transform = CGAffineTransformMakeScale(0.6, 0.6);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:pulsesteps[0]];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(pulseGrowAnimationDidStop:finished:context:)];
    self.transform = CGAffineTransformMakeScale(1.1, 1.1);
    [UIView commitAnimations];
}

- (void)pulseGrowAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {  
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:pulsesteps[1]];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(pulseShrinkAnimationDidStop:finished:context:)];
    self.transform = CGAffineTransformMakeScale(0.9, 0.9);
    [UIView commitAnimations];
}

- (void)pulseShrinkAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:pulsesteps[2]];
    self.transform = CGAffineTransformIdentity;
    [UIView commitAnimations];
}

Comments are closed.