【问题标题】:Custom GIF through UIImageView on Xcode not appearing?通过 Xcode 上的 UIImageView 自定义 GIF 没有出现?
【发布时间】:2014-07-21 22:06:45
【问题描述】:

我是一名初级程序员,试图在 iOS 的 Xcode 上使用 UIImageView 创建自定义 GIF。我有下面的代码,但它根本没有显示。我觉得解决方案非常简单,而且就在我面前,但我整天都在摸头,试图找出没有成功的问题。我在 sprite kit 中完成了几乎所有的游戏,并且在 MyScene 类(不是视图控制器)中完成。

    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];

【问题讨论】:

    标签: ios animation uiimageview animated-gif


    【解决方案1】:

    我认为答案隐藏在您的问题中 - 您提到“我在 MyScene 类(而不是视图控制器)中执行此操作。”那么你认为 self.view 中的内容是什么?如果 self.view 不是控制器的视图,则它不会显示。

    将 testGIF 添加到您的 VC 的 self.view 中,您应该一切顺利

    【讨论】:

      【解决方案2】:

      一件事可能会导致加载问题,您的以下行:

      UIImage *GIFImage = [UIImage imageNamed:[NSString stringWithFormat:@"DragAndMatch-%i.png", i]];
      

      我认为应该是

      [NSString stringWithFormat:@"DragAndMatch-%d.png", i]];
      

      stringWithFormat 中的 %d 用于转换有符号的 32 位整数,例如 i 变量。试试看。此处提供了有关字符串格式说明符的更多信息:

      https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多