Thursday, September 06, 2012

Transparent PNG Animation


This is the code I am using to draw animations of explosions onto some UIImageViews.

I struggled a lot with "Animated PNG with Transparent Region" in this code, but in the end discovered that iPhone animation do not support PNG-8 and you must use PNG-24.


 NSMutableArray *frames = [NSMutableArray arrayWithCapacity:25];
    
    for (int i = 0; i < 26; i++) {
        UIImage *frame = [UIImage imageNamed:[NSString stringWithFormat:@"exp%d.png", i]];
        
        [frames addObject:frame];
    }
    
    self.playerShipImageAnim = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"exp0.png"]];
    self.hostileShipImageAnim = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"exp0.png"]];
    
    [playerShipImage addSubview:playerShipImageAnim];
    [hostileShipImage addSubview:hostileShipImageAnim];
    
    playerShipImageAnim.animationImages = frames;
    hostileShipImageAnim.animationImages = frames;
    playerShipImageAnim.animationRepeatCount = 1;
    hostileShipImageAnim.animationRepeatCount = 1;
    playerShipImageAnim.animationDuration = 0.5;
    hostileShipImageAnim.animationDuration = 0.5;