123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- //
- // TSCommonloadingView.swift
- // AIEmoji
- //
- // Created by 100Years on 2025/1/17.
- //
- import Kingfisher
- class TSCommonloadingView: TSBaseView {
-
- lazy var imageView: UIImageView = {
- let imageView = UIImageView.createImageView(imageName: "failed_big")
- imageView.isHidden = true
- return imageView
- }()
-
- lazy var animatedImageView: AnimatedImageView = {
- let animatedImageView = AnimatedImageView()
- animatedImageView.autoPlayAnimatedImage = false
- if let gifURL = Bundle.main.url(forResource: "rotatingAnimation", withExtension: "gif") {
- animatedImageView.kf.setImage(with: gifURL, options: [.cacheOriginalImage]) { result in
- switch result {
- case .success(let value):
- print("GIF 加载成功: \(value.source.url?.absoluteString ?? "")")
- case .failure(let error):
- print("GIF 加载失败: \(error.localizedDescription)")
- }
- }
- }
- return animatedImageView
- }()
-
- lazy var textLabel: UILabel = {
- let textLabel = UILabel.createLabel(font: .font(size: 14),textColor: .fromHex("#FFFFFF",alpha: 0.8))
- return textLabel
- }()
-
- private var targetView: UIView = UIView()
-
- override func creatUI() {
-
- contentView.addSubview(imageView)
- imageView.snp.makeConstraints { make in
- make.width.height.equalTo(200.0)
- make.centerX.equalToSuperview()
- make.top.equalToSuperview()
- }
-
- contentView.addSubview(animatedImageView)
- animatedImageView.snp.makeConstraints { make in
- make.width.height.equalTo(200.0)
- make.centerX.equalToSuperview()
- make.top.equalToSuperview()
- }
-
- contentView.addSubview(textLabel)
- textLabel.snp.makeConstraints { make in
- make.height.equalTo(18.0)
- make.top.equalTo(imageView.snp.bottom).offset(16)
- make.leading.bottom.trailing.equalToSuperview()
- }
- }
-
-
- override func dealThings() {
- // 监听应用生命周期事件
- NotificationCenter.default.addObserver(
- self,
- selector: #selector(handleAppDidEnterBackground),
- name: UIApplication.didEnterBackgroundNotification,
- object: nil
- )
-
- NotificationCenter.default.addObserver(
- self,
- selector: #selector(handleAppWillEnterForeground),
- name: UIApplication.willEnterForegroundNotification,
- object: nil
- )
- }
-
-
- lazy var isRotating = false{
- didSet{
- if isRotating == true{
- startRotating(view: imageView)
- }else{
- stopRotating(view: imageView)
- }
- }
- }
-
-
- /// 开始旋转
- func startRotating(view:UIView,duration: Double = 2.0) {
- kDelayMainShort {
- self.animatedImageView.startAnimating()
- }
- }
- /// 停止旋转
- func stopRotating(view:UIView) {
- animatedImageView.stopAnimating()
- }
-
- func showLoading(text:String){
- animatedImageView.isHidden = false
- imageView.isHidden = true
-
- textLabel.text = text
- isRotating = true
- }
-
- func showError(text:String){
- animatedImageView.isHidden = true
-
- imageView.isHidden = false
- imageView.image = UIImage(named: "failed_big")
-
- textLabel.text = text
- isRotating = false
- }
-
- @objc private func handleAppDidEnterBackground() {
- stopRotating(view: targetView)
- }
- @objc private func handleAppWillEnterForeground() {
- isRotating = isRotating
- }
- }
|