在Swift中通过各种图像制作动画-使用自定义的加载视图

问题描述 投票:0回答:1

我有一个customLoader,我想在用户登录时随时调用。我想通过gifArray中的所有七张照片制作动画。

import UIKit

class CustomLoader: UIView {

static let instance = CustomLoader()
let gifArray: [UIImage] = [UIImage(named: "beer1")!,UIImage(named: "beer2")!,UIImage(named: "beer3")!,UIImage(named: "beer4")!,UIImage(named: "beer5")!,UIImage(named: "beer6")!,UIImage(named: "beer7")!]

lazy var transparentView:UIView = {
    let transparentView = UIView(frame: UIScreen.main.bounds)
    transparentView.backgroundColor = UIColor.black.withAlphaComponent(0.7)
    transparentView.isUserInteractionEnabled = false
    return transparentView
} ()

lazy var gifImage: UIImageView = {
    var gifImage = UIImageView(frame: CGRect(x: 0, y: 0, width: 200.00, height: 100))
    gifImage.contentMode = .scaleAspectFit
    gifImage.center = transparentView.center
    gifImage.isUserInteractionEnabled = false

    return gifImage
} ()



func animate() {
    for image in gifArray {
    gifImage.image = image
    gifImage.animationRepeatCount = 1
    gifImage.startAnimating()
    }
}

func showLoader() {
    self.addSubview(transparentView)
    self.transparentView.addSubview(gifImage)
    self.transparentView.bringSubviewToFront(self.gifImage)
   animate()

}


func hideLoader() {
    self.transparentView.removeFromSuperview()
}
}

我在用户登录时称呼它

    In ViewDidLoad:
    CustomLoader.instance.showLoader()
    view.addSubview(CustomLoader.instance.transparentView)
    view.bringSubviewToFront(CustomLoader.instance.transparentView)

问题:很遗憾,它仅显示了最后一张图像。我该如何解决?

ios swift animation gif viewdidload
1个回答
0
投票

这里的for循环设置了最后一张图片

for image in gifArray {

代替您需要

gifImage.isUserInteractionEnabled = false
gifImage.animationImages = gifArray

然后

func animate() { 
  gifImage.startAnimating()
}
© www.soinside.com 2019 - 2024. All rights reserved.