调用 swift 中的内部函数
call function which inside function in swift
我有一个在我的视图中为微调器设置动画的函数,在这个函数中我添加了两个函数来在需要时启动和停止动画,我需要在这个主函数中调用这个函数,有没有办法实现是吗?
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()
}
}
我认为更好的方法是将 UI 元素声明为 UIViewController 的 属性,配置此元素,然后使用两个函数来在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()
}
}
您可以使用扩展来添加加载视图和调用开始或停止动画
用于在 viewController:
中找到带有标签的 NVActivityIndicatorView
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()
}
}
并像这样使用:
public class ViewController: UIViewController {
public override func viewDidLoad() {
super.viewDidLoad()
self.addLoadingView()
self.stopLoading()
self.startLoading()
}
}
我有一个在我的视图中为微调器设置动画的函数,在这个函数中我添加了两个函数来在需要时启动和停止动画,我需要在这个主函数中调用这个函数,有没有办法实现是吗?
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()
}
}
我认为更好的方法是将 UI 元素声明为 UIViewController 的 属性,配置此元素,然后使用两个函数来在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()
}
}
您可以使用扩展来添加加载视图和调用开始或停止动画 用于在 viewController:
中找到带有标签的 NVActivityIndicatorViewextension 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()
}
}
并像这样使用:
public class ViewController: UIViewController {
public override func viewDidLoad() {
super.viewDidLoad()
self.addLoadingView()
self.stopLoading()
self.startLoading()
}
}