PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 17, 2022

[FIXED] How do I scale an animated GIF in Swift?

 July 17, 2022     animation, autolayout, gif, ios, swift     No comments   

Issue

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)
    }

}

Solution

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.



Answered By - David Reich
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing