刷新控件不动画 - UItableView

时间:2018-04-02 16:36:12

标签: ios uitableview uiapplication uirefreshcontrol

我有一个UITableView,它将refreshControl添加为

func addRefreshControl() {
    refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
    testTableView.addSubview(refreshControl)
}

当我向下滚动tableView时,refreshControl开始制作动画。但是当我按下主页按钮并且应用程序进入暂停状态时,刷新控制将停止。

恢复动画我做了如下:

     func applicationDidBecomeActive(_ application: UIApplication) {
    let vc = window?.rootViewController as! ViewController
    if vc.refreshControl.isRefreshing {
        vc.refreshControl.endRefreshing()
        vc.refreshControl.beginRefreshing()
        vc.testTableView.setContentOffset(CGPoint(x:0,y:vc.testTableView.contentOffset.y - vc.refreshControl.frame.size.height) , animated: true)
    }
}

但刷新控件无法启动。 Here是示例项目,我已附上以供快速参考。

当应用程序从暂停状态唤醒时,我该怎么做才能启动动画?

2 个答案:

答案 0 :(得分:0)

在AppDelegate中,你有标准:

eth0

注意评论。

然后你有方法:

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

因此,当您按下主页按钮,通过多任务处理从一个应用程序切换到另一个应用程序或按开/关按钮锁定该按钮时,将调用第一个方法。

第二个在过渡期间被称为 从背景状态到前景。

您可能想要使用的是:

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

在转换状态之后发生的情况,应用程序处于前台模式。

完成后,您的代码中还有另一个问题,即您要实例化视图控制器的新实例:

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

这意味着您拥有let vc = window?.rootViewController as! ViewController if vc.refreshControl.isRefreshing { vc.refreshControl.endRefreshing() vc.refreshControl.beginRefreshing() vc.testTableView.setContentOffset(CGPoint(x:0,y:vc.testTableView.contentOffset.y - vc.refreshControl.frame.size.height) , animated: true) } 的新实例,因此尚未令人耳目一新。在哪里引导你的代码永远不会工作。

您可以在下面找到完整的代码,以便在实施后使其正常运行。请记住,有不同的方法可以在没有通知的情况下制作它。 您可以使用示例refreshControl方法。但我正在使用委托作为你的代码陈述。

您的视图控制器:

viewWillAppear

你的代表:

//
//  ViewController.swift
//  TestProject
//
//  Created by Anish on 4/2/18.
//  Copyright © 2018 Costello. All rights reserved.
//

import UIKit

var refreshControl = UIRefreshControl()

class ViewController: UIViewController {

    @IBOutlet weak var testTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        testTableView.dataSource = self
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.resumeRefreshing), name: NSNotification.Name(rawValue: "resumeRefreshing"), object: nil)
        addRefreshControl()
    }


    @objc func addRefreshControl() {
        refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
        refreshControl.tag = 1
        testTableView.addSubview(refreshControl)
    }

    @objc func resumeRefreshing() {
        refreshControl.endRefreshing()
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            refreshControl.beginRefreshing()
            self.testTableView.setContentOffset(CGPoint(x:0,y:-60) , animated: true)
        }
    }

    @objc func refresh() {
        print("isRefreshing")
    }

}

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "Hello"
        return cell
    }

}

答案 1 :(得分:0)

以下代码对我有用:

    func applicationDidBecomeActive(_ application: UIApplication) {
    let vc = self.window?.rootViewController as! ViewController
    vc.testTableView.setContentOffset(.zero , animated: true)
    if vc.refreshControl.isRefreshing {
        vc.refreshControl.endRefreshing()
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
            vc.testTableView.setContentOffset(CGPoint(x: 0, y: -vc.refreshControl.frame.size.height), animated: true)
            vc.refreshControl.beginRefreshing()
        }
    }
}