[英]Adding a subview inside a view which is a subview of Window
我正在嘗試使用Lottie
Animations 創建自定義IndicatorView
。 我在UIApplication.shared.keyWindow?
它工作正常。 當我嘗試在IndicatorView
中添加另一個子視圖時,就會出現問題。
當我嘗試在loadingAnimation
上添加約束時,代碼在setupLoadingView()
中崩潰。
更新:感謝@Raghav7890。 有一個愚蠢的錯誤現在已經解決了。 我已經為任何想要使用 Lottie Animations 創建自定義指標視圖的人更新了答案。 玩得開心。
這是代碼:
import Foundation
import UIKit
import Lottie
class IndicatorView : UIView {
static let shared = IndicatorView()
var loadingAnimation : AnimationView = {
let lottieView = AnimationView()
lottieView.translatesAutoresizingMaskIntoConstraints = false
lottieView.layer.masksToBounds = true
return lottieView
}()
override init(frame: CGRect) {
super.init(frame: frame)
translatesAutoresizingMaskIntoConstraints = false
setupLoadingView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public func show() {
self.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
self.isHidden = false
self.alpha = 1
}, completion: nil)
applyLottieAnimation()
}
public func hide() {
removeLoadingView()
}
func applyLottieAnimation() {
let animationToShow = Animation.named("loading")
loadingAnimation.animation = animationToShow
loadingAnimation.animationSpeed = 1.0
loadingAnimation.loopMode = .loop
loadingAnimation.contentMode = .scaleAspectFill
loadingAnimation.play()
}
private func setupLoadingView() {
UIApplication.shared.keyWindow?.addSubview(self)
self.backgroundColor = UIColor.darkGray.withAlphaComponent(0.5)
guard let activeWindow = UIApplication.shared.keyWindow?.layoutMarginsGuide else {return}
self.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.size.height + 100).isActive = true
self.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.size.width).isActive = true
self.centerXAnchor.constraint(equalTo: activeWindow.centerXAnchor).isActive = true
self.centerYAnchor.constraint(equalTo: activeWindow.centerYAnchor).isActive = true
self.addSubview(loadingAnimation)
loadingAnimation.heightAnchor.constraint(equalToConstant: 100).isActive = true
loadingAnimation.widthAnchor.constraint(equalToConstant: 100).isActive = true
loadingAnimation.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
loadingAnimation.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
loadingAnimation.backgroundColor = UIColor(red: 48/255, green: 72/255, blue: 96/255, alpha: 1.0)
loadingAnimation.applyCornerRadius()
loadingAnimation.clipsToBounds = true
self.setNeedsLayout()
self.reloadInputViews()
}
func removeLoadingView() {
self.alpha = 1
UIView.animate(withDuration: 0.5, animations: {
self.alpha = 0
self.loadingAnimation.stop()
}, completion: { _ in
self.isHidden = true
})
}
}
嘗試像這樣使用您的財產
var loadingAnimation : AnimationView = {
let lottieView = AnimationView()
lottieView.translatesAutoresizingMaskIntoConstraints = false
// lottieView.contentMode = .scaleAspectFit
lottieView.layer.masksToBounds = true
return lottieView
}()
var loadingLabel : UILabel = {
let label = UILabel()
label.text = loadingText
label.textColor = .white
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont(name: "SegoeUI", size: 12)
return label
}()
由於您的方法每次訪問時都會生成一個新的 object
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.