协议可重复使用的旋转器一个Liner ProgressView
这是我目前的代码,而不是使用progressView
作为
self.progressview = showprogressview()
我想将其用作self.self.ser.showprogress()作为一个衬里,
这是我当前的协议
protocol Loadable {
var progressView: ProgressView? { get }
func showProgressView() -> ProgressView
func hideProgressView()
}
extension Loadable where Self: UIViewController {
func showProgressView() -> ProgressView {
let pv = ProgressView(frame: CGRect.zero)
view.addSubview(pv)
pv.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(pv.setConstraints(boundsOf: view))
UIView.animate(withDuration: 0.3) {
pv.alpha = 1.0
}
return pv
}
func hideProgressView() {
guard let progressView = progressView else { return }
UIView.animate(withDuration: 0.6) {
progressView.alpha = 0.0
} completion: { finished in
progressView.removeFromSuperview()
}
}
}```
//this is my current usage on the code
@objc private func loadData() {
print("Calling API")
self.progressView = showProgressView()
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0){
self.civilizationData()
self.hideProgressView()
}
}
//tried using this method
protocol Loadable {
var progressView: ProgressView? { get }
func showProgressView()
func hideProgressView()
}
extension Loadable where Self: UIViewController {
func showProgressView() {
let progressView = ProgressView(frame: CGRect.zero)
view.addSubview(progressView)
progressView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(progressView.setConstraints(boundsOf: view))
UIView.animate(withDuration: 0.3) {
progressView.alpha = 1.0
}
}
//but the added subview of my progressview doesn't disappear
protocol Loadable {
var progressView: ProgressView? { get }
func showProgressView()
func hideProgressView()
}
extension Loadable where Self: UIViewController {
func showProgressView() {
let progressView = ProgressView(frame: CGRect.zero)
view.addSubview(progressView)
progressView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(progressView.setConstraints(boundsOf: view))
UIView.animate(withDuration: 0.3) {
progressView.alpha = 1.0
}
}
This is my code at the moment I instead of using progressView
as
self.progressView = showProgressView()
I want to use it as self.showProgress()
as a one liner
this is my current protocol
protocol Loadable {
var progressView: ProgressView? { get }
func showProgressView() -> ProgressView
func hideProgressView()
}
extension Loadable where Self: UIViewController {
func showProgressView() -> ProgressView {
let pv = ProgressView(frame: CGRect.zero)
view.addSubview(pv)
pv.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(pv.setConstraints(boundsOf: view))
UIView.animate(withDuration: 0.3) {
pv.alpha = 1.0
}
return pv
}
func hideProgressView() {
guard let progressView = progressView else { return }
UIView.animate(withDuration: 0.6) {
progressView.alpha = 0.0
} completion: { finished in
progressView.removeFromSuperview()
}
}
}```
//this is my current usage on the code
@objc private func loadData() {
print("Calling API")
self.progressView = showProgressView()
DispatchQueue.main.asyncAfter(deadline: .now() + 3.0){
self.civilizationData()
self.hideProgressView()
}
}
//tried using this method
protocol Loadable {
var progressView: ProgressView? { get }
func showProgressView()
func hideProgressView()
}
extension Loadable where Self: UIViewController {
func showProgressView() {
let progressView = ProgressView(frame: CGRect.zero)
view.addSubview(progressView)
progressView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(progressView.setConstraints(boundsOf: view))
UIView.animate(withDuration: 0.3) {
progressView.alpha = 1.0
}
}
//but the added subview of my progressview doesn't disappear
protocol Loadable {
var progressView: ProgressView? { get }
func showProgressView()
func hideProgressView()
}
extension Loadable where Self: UIViewController {
func showProgressView() {
let progressView = ProgressView(frame: CGRect.zero)
view.addSubview(progressView)
progressView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(progressView.setConstraints(boundsOf: view))
UIView.animate(withDuration: 0.3) {
progressView.alpha = 1.0
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论