garethdn
garethdn

Reputation: 12351

Replacing static UIImageView with animation

I have a UImageViewwhich i have set up in interface builder with a png (pair of eyes) from my resources. I then want to replace this image (after a specific amount of time) with an animation of eyes blinking.

This is the code i have used which is called in viewWillAppear:

NSString *fileName; 
    NSMutableArray *imageArray = [[NSMutableArray alloc] init];
    for(int i = 1; i < 12; i++) {
        fileName = [NSString stringWithFormat:@"HDBlinkPage1/hd_eyes_blinking%d.png", i];
        [imageArray addObject:[UIImage imageNamed:fileName]];
    }
    imgHDBlink.userInteractionEnabled = YES;
    imgHDBlink.animationImages = imageArray;
    imgHDBlink.animationDuration = 0.9;
    imgHDBlink.animationRepeatCount = 1;
    imgHDBlink.contentMode = UIViewContentModeScaleToFill;
    //[self.view addSubview:imgHDBlink];
    [imgHDBlink startAnimating];

In viewWillAppear I use an NSTimer to trigger the animation every 5 seconds:

[NSTimer scheduledTimerWithTimeInterval:5.0
                                     target:self
                                   selector:@selector(blinkAnimation)
                                   userInfo:nil
                                    repeats:YES];

The problem is, when i run the application i don't see the initial static image at all. I just see the animation every 5 seconds but no image of the open eyes in between these animations. Can anyone please help me resolve this issue or point me in the right direction? Thanks.

Upvotes: 1

Views: 540

Answers (1)

Paul de Lange
Paul de Lange

Reputation: 10633

Add the animation images after 5.0 seconds. From the UIImageView docs:

The array must contain UIImage objects. You may use the same image object more than once in the array. Setting this property to a value other than nil hides the image represented by the image property. The value of this property is nil by default.

If you set the animationImages array beforehand, it won't display the image.

EDIT: (All using ARC)

- (void) viewDidLoad {
  [super viewDidLoad];

  //Initialize self.imgHDBlink
}

- (void) viewDidAppear: (BOOL) animated {
    [super viewDidAppear: animated];

    self.imgHDBlink.image = [UIImage imageNamed: @"static_image"];

    [NSTimer scheduledTimerWithTimeInterval: 5.0
                                     target: self
                                   selector: @selector(blinkAnimation:)
                                   userInfo: nil
                                    repeats: YES];
}

- (void) blinkAnimation: (NSTimer*) timer {

    self.imgHDBlink.animationImages = [NSArray array];  //Actually add your images here
    [self.imgHDBlink startAnimating];

    [self.imgHDBlink performSelector: @selector(setAnimationImages:) withObject: nil afterDelay: self.imgHDBlink.animationDuration];
}



//Remember this to stop crashes if we are dealloced
- (void) dealloc {
    [NSObject cancelPreviousPerformRequestsWithTarget: self 
                                             selector: @selector(blinkAnimation:) 
                                               object: nil];
}

Upvotes: 1

Related Questions