如何在Swift中缩放动画GIF?

时间:2015-09-07 05:12:14

标签: ios swift animation autolayout gif

我正在使用此功能为此回购设备制作GIF动画,但效果很好。 https://github.com/bahlo/SwiftGif

我的问题是我不知道如何在我的场景中缩放它来处理多个iOS设备。似乎所有的动画都是用代码完成的,我想知道是否有办法将它连接到Storyboard中的UIImageView,这样我就可以使用自动布局了。

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

}

1 个答案:

答案 0 :(得分:1)

我刚刚做了一个例子并且没有任何问题 我做了以下几点:

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

就是这样。工作正常。
将其包装到IBDesignable类中是一项留给读者的练习。