skip to Main Content

I have a function to animate spinner inside my view, and inside this function I added two functions to start and stop the animation whenever needed, I need to call the function inside this main function, is there a way to achieve it?

override func viewDidLoad() {
        super.viewDidLoad()
   
        SpinnerAnimiation()

    }
   

    fileprivate func SpinnerAnimiation() {
            let loading = NVActivityIndicatorView(frame: .zero, type: .ballBeat, color: .blue, padding: 0)
            loading.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(loading)
            NSLayoutConstraint.activate([
                loading.widthAnchor.constraint(equalToConstant: 40),
                loading.heightAnchor.constraint(equalToConstant: 40),
                loading.centerYAnchor.constraint(equalTo: view.centerYAnchor),
                loading.centerXAnchor.constraint(equalTo: view.centerXAnchor)
            ])
            
            func start() {
                loading.startAnimating()
            }
            
            func stop() {
                loading.stopAnimating()
            }
         
        }

2

Answers


  1. I think the better way to do it is declare UI element as a property of the UIViewController, configure this element and then have two functions to operate it inside UIViewController.

    import UIKit
    
    class MyViewController: UIViewController {
       private let loading = NVActivityIndicatorView(frame: .zero, type: .ballBeat, color: .blue, padding: 0)
    
       private func configureLoading() {
           loading.translatesAutoresizingMaskIntoConstraints = false
                view.addSubview(loading)
                NSLayoutConstraint.activate([
                    loading.widthAnchor.constraint(equalToConstant: 40),
                    loading.heightAnchor.constraint(equalToConstant: 40),
                    loading.centerYAnchor.constraint(equalTo: view.centerYAnchor),
                    loading.centerXAnchor.constraint(equalTo: view.centerXAnchor)
                ])
       }
    
       override func viewDidLoad() {
            super.viewDidLoad()
    
            configureLoading()
       }
    
       private func start() {
          loading.startAnimating()
       }
                
       private func stop() {
           loading.stopAnimating()
       }
         
    }
    
    Login or Signup to reply.
  2. you can use extension for add loading view and call start or stop animation
    for find NVActivityIndicatorView with tag inside viewController:

    extension UIViewController {
        
        public func addLoadingView() {
            
            // check view with tag
            guard self.view.viewWithTag(9876) == nil else {
                return
            }
            
            let loading = NVActivityIndicatorView(frame: .zero, type: .ballBeat, color: .blue, padding: 0)
            loading.translatesAutoresizingMaskIntoConstraints = false
            
            // set custom tag for prevent add twice
            loading.tag = 9876
            
            self.view.addSubview(loading)
            NSLayoutConstraint.activate([
                loading.widthAnchor.constraint(equalToConstant: 40),
                loading.heightAnchor.constraint(equalToConstant: 40),
                loading.centerYAnchor.constraint(equalTo: view.centerYAnchor),
                loading.centerXAnchor.constraint(equalTo: view.centerXAnchor)
            ])
        }
        
        public func startLoading() {
            guard let loadingView = self.view.viewWithTag(9876) as? UIView else {
                return
            }
            
            loadingView.startAnimation()
        }
        
        public func stopLoading() {
            guard let loadingView = self.view.viewWithTag(9876) as? UIView else {
                return
            }
            
            loadingView.stopAnimation()
        }
    }
    

    and Use like this:

    public class ViewController: UIViewController {
        
        public override func viewDidLoad() {
            super.viewDidLoad()
            
            self.addLoadingView()
            self.stopLoading()
            self.startLoading()
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search