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
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.
you can use extension for add loading view and call start or stop animation
for find NVActivityIndicatorView with tag inside viewController:
and Use like this: