1

私は迅速に新しいです。私はそれをどうやってできるのか理解できません。基本的には、コレクションビューですべてのデータが読み込まれた後、スクロールしてからuiactivityインジケーターを表示しようとすると、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() 


     } 

答えて

0

あなたのコードに次の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) 
+0

を私はそれが何のエラーかを示すが作業していないされていません – cristainlika3

関連する問題