如何继续对视图进行动画刷新控制已更改

时间:2016-06-30 14:24:54

标签: ios swift tableview refresh controls

我有一张桌子,#34; A"它位于标签栏控制器内。当我向下滚动以重新加载表刷新控件时开始动画。当按Tab键控制器进入视图" B"然后返回查看" A",刷新控件是可见的,但不是动画。

有人能帮助我吗?感谢

func refresh(sender:AnyObject)
{
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in

    Call API for data
    dispatch_async(dispatch_get_main_queue(), { () -> Void in

                UPDATE UI
                refreshControl.endRefreshing()

            })
        });
}

override func viewDidAppear(animated: Bool)
{
    if(requestTerminated == false)
    {
       //Continue animate refresh control somehow
    }
}

1 个答案:

答案 0 :(得分:1)

假设您在控制器A中遇到这种情况,例如viewDidLoad

//global var
var refreshControl:UIRefreshControl!

self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "pull to refresh")
self.refreshControl.addTarget(self, action: #selector(MyTableViewController.pullToRefresh(_:)), forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refreshControl)

您可以看到将refreshControl添加为A表控制器的子视图。当你去另一个控制器时,可能会出现另一个控制器(B),当你回到A时,你的刷新会被冻结或卡住。

您可以在UIRefreshControl上启动viewWillAppear动画并在viewDidDisappear结束动画。在转换期间,保存刷新过程的状态以了解何时显示UIRefreshControl

你可以使用这样的布尔值:

// Refresh state
var isRefreshing = false

// Call on viewWillApper
func superviewWillApper(){
    if isRefreshing && !refreshControl.refreshing{
        startRefreshAnimation()
    }
}

// Call on viewDidDisapper
func superviewDidDisappear(){
    endRefreshAnimation(false, dataFetched: !isRefreshing)
}

func startRefreshAnimation(){
    refreshControl.beginRefreshing()
    contentOffset = CGPointMake(0, -refreshControl.bounds.size.height)

    isRefreshing = true
}

//Saves state of refresh
func endRefreshAnimation(wasEmpty: Bool, dataFetched: Bool){
    refreshControl.endRefreshing()
    isRefreshing = !dataFetched
    if !wasEmpty{
        setContentOffset(CGPointZero, animated: true)
    }else{
        setContentOffset(CGPointZero, animated: false)
    }
}