Search code examples
iosanimationuiimageviewanimated-gif

Custom GIF through UIImageView on Xcode not appearing?


I am a beginning programmer trying to create a custom GIF using the UIImageView on iOS's Xcode. I have the code below, but it is not displaying at all. I feel like the solution is very simple and right in front of me but I've been hitting my head all day trying to figure out what's wrong with no success. I am doing almost all of this game in sprite kit and am doing this in the MyScene class (not the view controller).

    UIImageView *testGIF= [[UIImageView alloc] initWithFrame:CGRectMake(self.size.width/2,self.size.height/4,100,100)];
    NSMutableArray *GIFImageArray = [@[] mutableCopy];
    NSString *GIFImageNames[15];
    for (int i = 1; i <=15; i++) {
        UIImage *GIFImage = [UIImage imageNamed:[NSString stringWithFormat:@"DragAndMatch-%i.png", i]];
        GIFImageNames[i-1]=[NSString stringWithFormat:@"DragAndMatch-%i.png", i];
        [GIFImageArray addObject:GIFImage];
    }

    testGIF.animationImages = GIFImageArray;
    testGIF.animationRepeatCount = 1;
    testGIF.animationDuration = 5;

    [self.view addSubview: testGIF];
    [testGIF startAnimating];

Solution

  • I think the answer is hidden in your question - you mentioned "am doing this in the MyScene class (not the view controller)." what do you think is in self.view then? if self.view is not the view of the controller, then its not going to show.

    Add the testGIF to your VC's self.view and you should be good to go