如果我在viewDidLoad之后以编程方式设置UITableView页脚视图,则它不会设置动画

时间:2019-09-05 15:15:41

标签: swift uitableview

我在UIViewController中有一个UITableView。 UIViewController具有三个实例变量:

@IBOutlet weak var tableView: UITableView!
var loadingIndicatorFooterView: UIView!
var searchAgainPromptFooterView: UIView!

此表用于显示搜索结果。有时我想显示loadingIndicatorFooterView(分页结果),有时我想显示searchAgainPromptFooterView(没有更多结果要显示)。

我的loadingIndicatorFooterView是车轮的旋转图像,因此实现了旋转:

class SpinnerView : UIImageView {
    convenience init() {
        self.init(image: UIImage(named: "Spinner"))
        rotateInfinitely()
    }

    func rotateInfinitely() {
        let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotationAnimation.fromValue = 0.0
        rotationAnimation.toValue = Double.pi * 2 //Minus can be Direction
        rotationAnimation.duration = 1
        rotationAnimation.repeatCount = .infinity
        self.layer.add(rotationAnimation, forKey: nil)
    }
}

我初始化loadingIndicatorFooterViewsearchAgainPromptFooterView并将其存储在视图控制器的viewDidLoad函数中。如果在tableView.tableFooterView中将loadingIndicatorFooterView设置为viewDidLoad,我确实会看到轮子在旋转。但是我暂时没有设置tableView.tableFooterView,所以它是nil

稍后,当我执行搜索时,在搜索功能中,该行如下:

tableView.tableFooterView = loadingIndicatorFooterView

我确实看到了轮子,但它没有旋转。

这是我的代码,用于实例化SpinnerView并将其存储到loadingIndicatorFooterView中的viewDidLoad中:

loadingIndicatorFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 50))
let loadingIndicator = SpinnerView()
loadingIndicator.translatesAutoresizingMaskIntoConstraints = false
loadingIndicatorFooterView.addSubview(loadingIndicator)
// < add constraints >

1 个答案:

答案 0 :(得分:0)

下一个类微调框可以是

class Spinner: UIViewController {
    var spinner = UIActivityIndicatorView(style: .whiteLarge)

    override func loadView() {
        view = UIView()
        view.backgroundColor = UIColor(white: 0.5, alpha: 0.5)

        spinner.translatesAutoresizingMaskIntoConstraints = false
        spinner.startAnimating()
        view.addSubview(spinner)

        spinner.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        spinner.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    }
}

您应该创建您的微调器,请参见计时器DispatchQueue.main.asyncAfter(deadline:.now()+ 12){

func createSpinnerView() {
        let child = Spinner()

        self.view.addSubview(child.view)
        child.view.frame = CGRect(x: 100, y: 400, width: 100, height: 100)
        child.didMove(toParent: self)
        DispatchQueue.main.asyncAfter(deadline: .now() + 12) {

            child.willMove(toParent: nil)
            child.view.removeFromSuperview()
            child.removeFromParent()
        }
    }

和您的viewDidLoad

override func viewDidLoad() {
        super.viewDidLoad()

        self.createSpinnerView()
}