首页 > 解决方案 > 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.

标签: iosswiftuiscrollviewuipagecontroluirefreshcontrol

解决方案


推荐阅读