在 UIImageView 中加载动画 gif IOS
Load Animated gif in UIImageView IOS
我的资产文件中有这个 gif
资产的名称是 loading_apple,但是当我添加以下代码时,出现空指针异常错误:
let img = UIImage (named: "loading_apple")
那么我怎样才能显示这个 gif? :(
希望有人能帮助我。
我建议拆分该 gif 的框架并使用 animatedImageNamed:duration:
- 您可以将它们命名为相似的名称,最后更改数字。例如:
loading-1.png
loading-2.png
loading-3.png
等
Xcode 会识别出您需要多张图片并按顺序播放。
看看THIS
我会推荐使用 FLAnimatedImage https://github.com/Flipboard/FLAnimatedImage
而不是存储 gif 文件,你为什么不使用 CAReplicatorLayer
参考这个 link and this link in objective c,我在第二次使用相同的代码并进行了一些修改,
func spinTheCustomSpinner() -> Void {
let aBar:CALayer = CALayer.init()
aBar.bounds = CGRectMake(0, 0, 8, 25);
aBar.cornerRadius = 4; //(8/2)
aBar.backgroundColor = UIColor.blackColor().CGColor
aBar.position = CGPointMake(150.0, 150.0 + 35)
let replicatorLayer:CAReplicatorLayer = CAReplicatorLayer.init()
replicatorLayer.bounds = CGRectMake(0, 0,300,300)
replicatorLayer.cornerRadius = 10.0
replicatorLayer.backgroundColor = UIColor.whiteColor().CGColor
replicatorLayer.position = CGPointMake(CGRectGetMidX(self.view!.bounds), CGRectGetMidY(self.view!.bounds))
let angle:CGFloat = CGFloat (2.0 * M_PI) / 12.0
let transform:CATransform3D = CATransform3DMakeRotation(angle, 0, 0, 1.0)
replicatorLayer.instanceCount = 12
replicatorLayer.instanceTransform = transform
replicatorLayer .addSublayer(aBar)
self.view!.layer .addSublayer(replicatorLayer)
aBar.opacity = 0.0
let animationFade:CABasicAnimation = CABasicAnimation(keyPath: "opacity")
animationFade.fromValue = NSNumber(float: 1.0)
animationFade.toValue = NSNumber(float: 0.0)
animationFade.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animationFade.repeatCount = HUGE
animationFade.duration = 1.0
let aBarAnimationDuration:Double = 1.0/12.0
replicatorLayer.instanceDelay = aBarAnimationDuration
aBar .addAnimation(animationFade, forKey: "fadeAnimation")
}
如果您将上面的视图与视图一起使用,在加载时显示此视图并在加载后隐藏,这真的很酷而且很方便,并且没有存储 gif 文件和使用图像视图加载的麻烦。
并且通过更改 aBar
图层的属性,您会得到不同的效果。
对于这个特定的 gif,您可能想使用 UIActivityIndicatorView
并使用 startAnimating()
使其具有动画效果
我的资产文件中有这个 gif
资产的名称是 loading_apple,但是当我添加以下代码时,出现空指针异常错误:
let img = UIImage (named: "loading_apple")
那么我怎样才能显示这个 gif? :(
希望有人能帮助我。
我建议拆分该 gif 的框架并使用 animatedImageNamed:duration:
- 您可以将它们命名为相似的名称,最后更改数字。例如:
loading-1.png
loading-2.png
loading-3.png
等
Xcode 会识别出您需要多张图片并按顺序播放。
看看THIS
我会推荐使用 FLAnimatedImage https://github.com/Flipboard/FLAnimatedImage
而不是存储 gif 文件,你为什么不使用 CAReplicatorLayer
参考这个 link and this link in objective c,我在第二次使用相同的代码并进行了一些修改,
func spinTheCustomSpinner() -> Void {
let aBar:CALayer = CALayer.init()
aBar.bounds = CGRectMake(0, 0, 8, 25);
aBar.cornerRadius = 4; //(8/2)
aBar.backgroundColor = UIColor.blackColor().CGColor
aBar.position = CGPointMake(150.0, 150.0 + 35)
let replicatorLayer:CAReplicatorLayer = CAReplicatorLayer.init()
replicatorLayer.bounds = CGRectMake(0, 0,300,300)
replicatorLayer.cornerRadius = 10.0
replicatorLayer.backgroundColor = UIColor.whiteColor().CGColor
replicatorLayer.position = CGPointMake(CGRectGetMidX(self.view!.bounds), CGRectGetMidY(self.view!.bounds))
let angle:CGFloat = CGFloat (2.0 * M_PI) / 12.0
let transform:CATransform3D = CATransform3DMakeRotation(angle, 0, 0, 1.0)
replicatorLayer.instanceCount = 12
replicatorLayer.instanceTransform = transform
replicatorLayer .addSublayer(aBar)
self.view!.layer .addSublayer(replicatorLayer)
aBar.opacity = 0.0
let animationFade:CABasicAnimation = CABasicAnimation(keyPath: "opacity")
animationFade.fromValue = NSNumber(float: 1.0)
animationFade.toValue = NSNumber(float: 0.0)
animationFade.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animationFade.repeatCount = HUGE
animationFade.duration = 1.0
let aBarAnimationDuration:Double = 1.0/12.0
replicatorLayer.instanceDelay = aBarAnimationDuration
aBar .addAnimation(animationFade, forKey: "fadeAnimation")
}
如果您将上面的视图与视图一起使用,在加载时显示此视图并在加载后隐藏,这真的很酷而且很方便,并且没有存储 gif 文件和使用图像视图加载的麻烦。
并且通过更改 aBar
图层的属性,您会得到不同的效果。
对于这个特定的 gif,您可能想使用 UIActivityIndicatorView
并使用 startAnimating()