OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 Collection View 的底部/页脚中添加 uiactivity 指示器? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:58
标题: ios - 如何在 Collection View 的底部/页脚中添加 uiactivity 指示器?

我是 swift 新手。我不知道我该怎么做。基本上我需要在 Collection View 加载所有数据时显示 uiactivity 指示器,然后您尝试滚动然后显示 uiactivity 指示器。这意味着从网络加载更多数据,请稍等。

我做了什么。

 ///define 
   var indicatorFooter : UIActivityIndicatorView!

   //set up  UIActivityIndicatorView to the collection view 
    override func setupViews() {

        indicatorFooter = UIActivityIndicatorView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(collectionView.frame.width), height: CGFloat(44)))
        indicatorFooter.color = UIColor.black

        collectionView.addSubview(indicatorFooter)

    }


    //end of the scroll view then load next 20 data from api 
     func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {

            // UITableView only moves in one direction, y axis
            let currentOffset = scrollView.contentOffset.y
            let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height

            // Change 10.0 to adjust the distance from bottom
            if maximumOffset - currentOffset <= 10.0 {
                self.loadMore()
            }

        }

        //request web to down load data 
        func loadMore(){
             //but it is not working  indicator 
             indicatorFooter.startAnimating()


        }



Best Answer-推荐答案


将以下 extension 添加到您的代码中:

extension UIView {

    // MARK: Activity Indicator

    func activityIndicator(show: Bool) {
        activityIndicator(show: show, style: .gray)
    }

    func activityIndicator(show: Bool, style: UIActivityIndicatorViewStyle) {
        var spinner: UIActivityIndicatorView? = viewWithTag(NSIntegerMax - 1) as? UIActivityIndicatorView

        if spinner != nil {
            spinner?.removeFromSuperview()
            spinner = nil
        }

        if spinner == nil && show {
            spinner = UIActivityIndicatorView.init(activityIndicatorStyle: style)
            spinner?.translatesAutoresizingMaskIntoConstraints = false
            spinner?.hidesWhenStopped = true
            spinner?.tag = NSIntegerMax - 1

            if Thread.isMainThread {
                spinner?.startAnimating()
            } else {
                DispatchQueue.main.async {
                    spinner?.startAnimating()
                }
            }

            insertSubview((spinner)!, at: 0)

            NSLayoutConstraint.init(item: spinner!, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0.0).isActive = true
            NSLayoutConstraint.init(item: spinner!, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0.0).isActive = true

            spinner?.isHidden = !show
        }
    }

}

要将微调器添加到您的 collectionView,您只需调用:

collectionView.activityIndicator(show: true)

关于ios - 如何在 Collection View 的底部/页脚中添加 uiactivity 指示器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44782527/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4