【问题标题】:How do I scale an animated GIF in Swift?如何在 Swift 中缩放动画 GIF?
【发布时间】:2015-12-02 14:02:31
【问题描述】:

我正在使用该功能从这个 repo 制作 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)
    }

}

【问题讨论】:

  • 你能分享你的代码吗?
  • 是的,很抱歉。在 Git Repo 中,有一个示例,但他似乎在 CGRect 中硬编码值,并且在较小的 iOS 设备的模拟器上无法很好地扩展。
  • 我假设你已经用你的 UIImageView 设置了故事板,所以不要创建一个新的 UIImageView 实例并将其添加为子视图,只需在现有动画 UIImageView 上设置 bouncingBall UIImage跨度>
  • 如果我这样做,它不会为 GIF 设置动画。我得到一张静态图片。

标签: ios swift animation autolayout gif


【解决方案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 类中是留给读者的练习。

【讨论】:

    猜你喜欢
    • 2012-02-26
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 2016-08-30
    • 2014-11-06
    相关资源
    最近更新 更多