ios - Add a refresh control to a horizontal scrollview with page control
问题描述
I am using a horizontal scrollview with paging like it can be seen here: https://medium.com/@anitaa_1990/create-a-horizontal-paging-uiscrollview-with-uipagecontrol-swift-4-xcode-9-a3dddc845e92
The refreshing works well from every imageView (subview), but the refreshing indicator is only shown when I am on the first imageView.
In the view did load function I add the refresh control to the scrollView
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(refreshOptions(sender:)), for: .valueChanged)
scrollView.refreshControl = refreshControl
Later I add the subviews with following function to the scrollView
// show the page Control if at least two specials are shown
if UrlArray.count > 1{
pageControl.isHidden = false
}
// add all images to the scrollView
for url in UrlArray {
// 1.
frame.origin.x = scrollView.frame.size.width * CGFloat(origin)
frame.size = scrollView.frame.size
// 2.
let imageView = UIImageView(frame: frame)
imageView.contentMode = .scaleAspectFit
// get the image for the url in the cache
if let cachedVersion = cache.object(forKey: url as NSString) {
imageView.image = cachedVersion.image
}
self.scrollView.addSubview(imageView) // add image to scrollView
// increase origin of next image by one
// that way next image is horizontally next to the one before
origin += 1
}
// 3.
scrollView.contentSize = CGSize(width: ((scrollView.frame.size.width) * CGFloat(origin)), height: (scrollView.frame.size.height))
scrollView.delegate = self
pageControl.numberOfPages = origin
And the function to call when the user swipes between images.
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
// update page number when view was paged/scrolled
let pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
pageControl.currentPage = Int(pageNumber)
}
I hope somebody can help me to get the refresh indicator to be shown at every subview.
解决方案
推荐阅读
- swift - Firestore 查询是否可以使用其中之一或 whereFields?
- javascript - 使用 javascript/jquery 创建和检查 cookie 值
- python - 使用 Tensorflow 对象检测 API 在单个图像中检测大量对象的问题
- r - 在函数中嵌套 shinyDownloadHandler()
- excel - 仅在列包含值时使用 SUBTOTAL
- node.js - 如果先调用 model.validate(),Mongoose 会保存无效数据而不会引发验证错误
- java - 在 IntelliJ 中创建 gradle 项目的问题
- c# - 设置任何窗口的高度、宽度和屏幕位置
- java - Servlet 找不到文件
- c# - 具有默认方法与特征的 C# 接口