gpt4 book ai didi

ios - 快速 UIActivityIndi​​catorView 而 NSURLConnection

转载 作者:搜寻专家 更新时间:2023-10-30 22:24:47 25 4
gpt4 key购买 nike

我知道如何为 UIActivityIndi​​catorView 设置动画我知道如何与 NSURLConnection.sendSynchronousRequest

建立连接

但我不知道如何在与 NSURLConnection.sendSynchronousRequest 建立连接时为 UIActivityIndi​​catorView 设置动画

谢谢

最佳答案

不要在主线程中使用sendSynchronousRequest(因为它会阻塞你运行它的任何线程)。您可以使用 sendAsynchronousRequest,或者考虑到 NSURLConnection 已弃用,您应该真正使用 NSURLSession,然后尝试使用 UIActivityIndi​​catorView 应该可以正常工作。

例如,在 Swift 3 中:

let indicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
indicator.center = view.center
view.addSubview(indicator)
indicator.startAnimating()

URLSession.shared.dataTask(with: request) { data, response, error in
defer {
DispatchQueue.main.async {
indicator.stopAnimating()
}
}

// use `data`, `response`, and `error` here
}

// but not here, because the above runs asynchronously

或者,在 Swift 2 中:

let indicator = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
indicator.center = view.center
view.addSubview(indicator)
indicator.startAnimating()

NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
defer {
dispatch_async(dispatch_get_main_queue()) {
indicator.stopAnimating()
}
}

// use `data`, `response`, and `error` here
}

// but not here, because the above runs asynchronously

关于ios - 快速 UIActivityIndi​​catorView 而 NSURLConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27591986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com