如何在 Swift 中缩放动画 GIF?
How do I scale an animated GIF in Swift?
我正在使用此功能为来自此存储库的 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. 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 class 是留给 reader 的练习。
我正在使用此功能为来自此存储库的 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. 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 class 是留给 reader 的练习。