如何在iOS Spritekit游戏中添加简单的动态GIF?

6

我是一名初级程序员,正在使用iOS sprite-kit创建游戏。我有一个简单的动画GIF(30帧),保存为.gif文件。是否有一种简单的方法(可能类似于通过UIImage添加常规.png的几行代码)在我的游戏中显示这个GIF?我已经研究了在Xcode中显示动画GIF的方法,大多数涉及导入大量类,其中大部分是我认为不需要的东西(我只知道足够去筛选它们)。


http://www.raywenderlich.com/45152/sprite-kit-tutorial-animations-and-texture-atlases - Zane Helton
2个回答

3
我认为GIF就像是动画精灵一样。因此,我会在一个for循环中将GIF添加为SKSpriteNode的纹理,然后使用SKAction.repeatActionForever()在设备上运行它。
说实话,我对此也比较新。我只是尽力给出最好的答案。这是用Swift编写的,但我认为将其翻译成Objective-C并不难。
var gifTextures: [SKTexture] = [];

    for i in 1...30 {
        gifTextures.append(SKTexture(imageNamed: "gif\(i)"));
    }

    gifNode.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(gifTextures, timePerFrame: 0.125)));

但是,一个动画GIF是一个单独的文件,而在您的代码中,您假设一系列单帧图像文件。 - Bobjt

2
迈克尔·蔡的答案可以帮你完成一半。剩下的部分是从gif文件中获取单个帧。这是我用Swift的方法:
func load(imagePath: String) -> ([SKTexture], TimeInterval?) {
    guard let imageSource = CGImageSourceCreateWithURL(URL(fileURLWithPath: imagePath) as CFURL, nil) else {
        return ([], nil)
    }

    let count = CGImageSourceGetCount(imageSource)
    var images: [CGImage] = []

    for i in 0..<count {
        guard let img = CGImageSourceCreateImageAtIndex(imageSource, i, nil) else { continue }
        images.append(img)
    }

    let frameTime = count > 1 ? imageSource.delayFor(imageAt: 0) : nil

    return (images.map { SKTexture(cgImage: $0) }, frameTime)
}

extension CGImageSource { // this was originally from another SO post for which I've lost the link. Apologies.

    func delayFor(imageAt index: Int) -> TimeInterval {
        var delay = 0.1

        // Get dictionaries
        let cfProperties = CGImageSourceCopyPropertiesAtIndex(self, index, nil)
        let gifPropertiesPointer = UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: 0)
        if CFDictionaryGetValueIfPresent(cfProperties, Unmanaged.passUnretained(kCGImagePropertyGIFDictionary).toOpaque(), gifPropertiesPointer) == false {
            return delay
        }

        let gifProperties: CFDictionary = unsafeBitCast(gifPropertiesPointer.pointee, to: CFDictionary.self)

        // Get delay time
        var delayObject: AnyObject = unsafeBitCast(
            CFDictionaryGetValue(gifProperties,
                                 Unmanaged.passUnretained(kCGImagePropertyGIFUnclampedDelayTime).toOpaque()),
            to: AnyObject.self)
        if delayObject.doubleValue == 0 {
            delayObject = unsafeBitCast(CFDictionaryGetValue(gifProperties,
                                                             Unmanaged.passUnretained(kCGImagePropertyGIFDelayTime).toOpaque()), to: AnyObject.self)
        }

        delay = delayObject as? TimeInterval ?? 0.1

        if delay < 0.1 {
            delay = 0.1 // Make sure they're not too fast
        }

        return delay
    }

}

请注意,我假设gif的每个帧都具有相同的长度,但并非总是如此。
您还可以使用这些图像相当轻松地构建SKTextureAtlas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接