ML.
ML.

Reputation: 599

How do I scale an animated GIF in Swift?

I'm using the functionality to animate a GIF from this repo and it works fine. https://github.com/bahlo/SwiftGif

My problem is that I don't know how to scale it in my scene to handle multiple iOS devices. It seems all the animation is done in code, and I want to know if there is a way to connect it to a UIImageView in Storyboard so I can use auto layout.

import UIKit
import SpriteKit

class TitlePage: UIViewController {

    @IBOutlet weak var animation: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        //Add the bouncing ball GIF
        var imageData = NSData(contentsOfURL: NSBundle.mainBundle()
            .URLForResource("Bouncing Ball", withExtension: "gif")!)
        let bouncingBall = UIImage.animatedImageWithData(imageData!)
        var animation = UIImageView(image: bouncingBall)
        //view.addSubview(animation)
    }

}

Upvotes: 1

Views: 3053

Answers (1)

David Reich
David Reich

Reputation: 779

I just made an example and have had no problems.
I did the following:

1. Created a Single-View Swift application project.
2. Added the UIImage+Gif.swift file to the project.
3. Added the adventure-time.gif file to the project.
4. In the Storyboard editor dragged a UIImageView to the middle of the main view.
5. Created an outlet for the UIImageView and named it "gifView".
6. Set constraints to center gifView horizontally and vertically.
7. Set more constraints to set gifView's H and W to 0.6 x container H and W.
8. In the ViewController added the following line to viewDidLoad():

gifView.image = UIImage.gifWithName("adventure-time")

That's it. Works fine.
Wrapping this into an IBDesignable class is an exercise left to the reader.

Upvotes: 2

Related Questions