我正在开发一个应用程序,希望我的滚动视图在加载数据时锁定在适当的位置。当前,当用户刷新时,滚动视图立即恢复到原位。如何调整代码,使滚动视图锁定到位,直到完全执行其中的代码?到目前为止,这是我的代码:
@IBOutlet weak var scrollView: UIScrollView!
var refreshControl: UIRefreshControl!
@objc func refreshData(sender: UIRefreshControl){
getItems()
sender.endRefreshing()
}
这是我的getItems()函数:
func getItems() {
//Hit the web service Url
let serviceUrl = "omitted"
let url = URL(string: serviceUrl)
//Download the json data
if let url = url{
//Create a URL Session
let session = URLSession(configuration: .default)
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 15.0)
let task = session.dataTask(with: request, completionHandler: {(data, response, error) in
if error == nil {
//Succeeded
//Call the parse json function on the data
self.parseJson(data!)
}
else {
print("error occured in getItems")
}
})
// Start the task
task.resume()
}
}
//Parse it out into DataModel structs
func parseJson(_ data:Data){
//parse the data into DataModel structs
do{
//parse the data into a json object
let jsonArray = try JSONSerialization.jsonObject(with: data, options: []) as! [Any]
//loop through each result in the json array
for jsonResult in jsonArray {
//Cast json result as a dictionary
let jsonDict = jsonResult as! [String:String]
//create new bar data and set its properties
let bardata = DataModel(name: jsonDict["Bar Name"]!, cover: jsonDict["Bar Cover"]!, deals: jsonDict["Drink Deals"]!)
//add it to the array
bar_info.append(bardata)
}
}
catch{
print("There was an error")
}
//call function here
update_cover()
}
func update_cover(){
var a_cover = "$5"
var b_cover = "$5"
var c_cover = "$5"
var d_cover = "$5"
for item in bar_info{
if item.name == "omitted"{
a = item.cover
}
else if item.name == "omitted"{
b_cover = item.cover
}
else if item.name == "omitted"{
c_cover = item.cover
}
else if item.name == "omitted"{
d_cover = item.cover
}
}
DispatchQueue.main.async {
self.aCoverView.text = a_cover
self.bCoverView.text = b_cover
self.cCoverView.text = c_cover
self.dCoverView.text = d_cover
}
}
答案 0 :(得分:0)
要锁定滚动视图,请使用 scrollView.isUserInteractionEnabled = false ,然后解锁 scrollView.isUserInteractionEnabled = true
答案 1 :(得分:0)
仅在重新滚动视图之前,存储当前位置的内容偏移量,然后在重新加载完成时对其进行设置。
var currentPos:CGPoint?
func reloadScrollview() {
currentPos = scrollview.contentOffset
// do your reloading stuff
// layout your subviews inside the scrollview
// set the content size based upon your subviews
scrollview.contentOffset = currentPos
}
快乐的编码。
答案 2 :(得分:0)
听起来您正在实现“按需刷新”,所以我假设您将要使用 #ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x601
#endif
委托方法:
scrollViewDidScroll
现在,您只需要配置该方法。
答案 3 :(得分:0)
您可以在getItems
中执行以下操作:
func getItems() {
//Hit the web service Url
let serviceUrl = "omitted"
let url = URL(string: serviceUrl)
//Download the json data
if let url = url{
//Create a URL Session
let session = URLSession(configuration: .default)
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 15.0)
let task = session.dataTask(with: request, completionHandler: {(data, response, error) in
DispatchQueue.main.async {
self.refreshControl.endRefreshing()
}
if error == nil {
//Succeeded
//Call the parse json function on the data
self.parseJson(data!)
}
else {
print("error occured in getItems")
}
})
// Start the task
task.resume()
}
}
仅在网络请求完成后(即在完成处理程序中),刷新控件才需要结束刷新。由于请求是异步的,因此getItems
在请求仍在进行时会立即返回。