xcode 在 iOS 10 上使用“self.refreshControl.endRefreshing()”刷新动画/旋转轮不会停止/消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38302413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh animation/spinning wheel doesn't stop/disappear with “self.refreshControl.endRefreshing()” on iOS 10
提问by Tam
This is my code:
这是我的代码:
func pullToRefresh() {
if Reachability().connectionStatus() == .Online {
self.homewebview.reload()
} else {
self.refreshControl.endRefreshing()
let alert = UIAlertView(title: "No Internet connection", message: "Please check your Internet connection.", delegate: nil, cancelButtonTitle: "Okay")
alert.show()
}
}
When internet connection is not available and the user pulls to refresh an alert should be shown and the animation should stop. That works great on iOS 9. But on iOS 10 Beta 2 the animation doesn't disappear. The user have to pull up to make it disappear. It that an iOS 10 bug or am I doing something wrong?
当 Internet 连接不可用且用户拉动刷新时,应显示警报并且动画应停止。这在 iOS 9 上效果很好。但在 iOS 10 Beta 2 上,动画不会消失。用户必须向上拉才能使其消失。是 iOS 10 错误还是我做错了什么?
P.S. I already tried to add assert(NSThread.isMainThread())
but that didn't help.
PS我已经尝试添加,assert(NSThread.isMainThread())
但这没有帮助。
回答by Matt Carr
I've just encountered this issue and for those using UIAlertController, you may wish to try something similar to the following:
我刚刚遇到了这个问题,对于那些使用 UIAlertController 的人,您可能希望尝试类似于以下内容:
[self presentViewController:alert animated:TRUE completion:^{
[self.refreshControl endRefreshing];
}];
This enables the loading spinner to disappear in the background as soon as the alert has been displayed.
这使加载微调器在显示警报后立即消失在后台。
Swift Equivalent:
斯威夫特等效:
present(alert, animated: true) {
self.refreshControl.endRefreshing()
}
回答by Tam
I also met the same issue. Just work around: please change like below
我也遇到了同样的问题。只是解决:请像下面一样改变
let alert = UIAlertView(title: "No Internet connection", message: "Please check your Internet connection.", delegate: nil, cancelButtonTitle: "Okay")
alert.show()
self.refreshControl.endRefreshing()
I search in Google and some guys talk about interfere when showing other view but not know the root cause
我在谷歌搜索,有些人在显示其他视图时谈论干扰但不知道根本原因
回答by Patrick
It had similar issues and solved it this way. I have a function to handle all fails (no connectivity, api errors) which is called as a notification, but I will simplify it here.
它有类似的问题并通过这种方式解决了它。我有一个函数来处理所有失败(无连接、api 错误),它被称为通知,但我会在这里简化它。
First I am calling endRefreshing
asynchronously (try without, you should see some errors in the console). Then I am delaying the alert 1 second, giving the refresh control enough time to end.
首先,我endRefreshing
异步调用(尝试不使用,您应该会在控制台中看到一些错误)。然后我将警报延迟 1 秒,让刷新控制有足够的时间结束。
func updatedStatusFailed(notification: NSNotification) {
DispatchQueue.main.async {
self.refresher.endRefreshing()
}
self.helpers.delay(1.0, closure: {
let alert = UIAlertController(title: "Error", message: "Error Description", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
})
}
My delay function comes from a helpers class because I use it a few times in my app.
我的延迟函数来自一个助手类,因为我在我的应用程序中使用了它几次。
func delay(_ delay:Double, closure:@escaping ()->()) {
let when = DispatchTime.now() + delay
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}
In many cases you may be calling an api when the view loads/appears and have the pull to refresh logic. In this case you might want to add this line so that you only delay by a second if the refresher is refreshing:
在许多情况下,您可能会在视图加载/出现时调用 api 并具有刷新逻辑。在这种情况下,您可能希望添加此行,以便在刷新器刷新时仅延迟一秒钟:
let delayTime:Double = self.refresher.isRefreshing ? 1.0 : 0.0
UPDATE: 20/1/16
更新:20/1/16
Although my somewhat hacky answer works, in the end I opted to have a section in my tableView to display errors, rather than the alert. It was a lot more code and complexity, but the UX is a lot better.
尽管我的回答有点老套,但最后我还是选择在我的 tableView 中有一个部分来显示错误,而不是警报。这是更多的代码和复杂性,但用户体验要好得多。
回答by Manab Kumar Mal
I think when we are presenting
the UIAlertController
at the same time refreshController
is trying to stop itself. That is why it is not getting time to stop refreshing and stays sometimes. So in my case I put endRefreshing
code after 1 second and its working smooth now.
我认为,当我们presenting
将UIAlertController
在同一时间refreshController
试图阻止自己。这就是为什么没有时间停止刷新并有时停留的原因。所以在我的情况下,我endRefreshing
在 1 秒后放置代码,现在它工作顺利。
Here is the code which I have used:
这是我使用的代码:
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.refreshControl.endRefreshing()
}