123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- //
- // TSGeneratorloadingView.swift
- // AIEmoji
- //
- // Created by 100Years on 2025/4/2.
- //
- import Kingfisher
- class TSGeneratorloadingView: 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 cusStackView: TSCustomStackView = {
- let cusStackView = TSCustomStackView(axis: .vertical,spacing: 0)
- cusStackView.scrollView.isScrollEnabled = false
- return cusStackView
- }()
-
- lazy var timeLabel: UILabel = {
- let textLabel = UILabel.createLabel(font: .font(size: 18,weight: .semibold),textColor: .white,textAlignment: .center)
- return textLabel
- }()
-
- lazy var textLabel: UILabel = {
- let textLabel = UILabel.createLabel(font: .font(size: 18),textColor: .white,textAlignment: .center,numberOfLines: 0)
- return textLabel
- }()
-
- lazy var infoLabel: UILabel = {
- let textLabel = UILabel.createLabel(font: .font(size: 14),textColor: .white.withAlphaComponent(0.6),textAlignment: .center,numberOfLines: 0)
- return textLabel
- }()
-
- private var targetView: UIView = UIView()
-
- // lazy var blurEffect: UIVisualEffectView = {
- // let blurEffect = createBlurEffectView(style: .dark)
- // blurEffect.alpha = 0.9
- // return blurEffect
- // }()
-
- lazy var blurEffect: TSDynamicBlurView = {
- return TSDynamicBlurView()
- }()
-
-
- lazy var regenerateBtn: UIButton = {
- let regenerateBtn = UIButton.createButton(title: "Regenerate".localized,backgroundImage: kSubmitBtnNormalbg,font: .font(size: 12),titleColor: "#111111".uiColor,corner: 22)
- // regenerateBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 28, bottom: 0, right:28)
- regenerateBtn.isHidden = true
- return regenerateBtn
- }()
-
- lazy var backgroundGenerateBtn: UIButton = {
- let btn = UIButton.createButton(title: "Generate in the background".localized,font: .font(size: 16),titleColor: .themeColor,corner: 24)
- btn.layer.borderColor = UIColor.themeColor.cgColor
- btn.layer.borderWidth = 1.0
- btn.titleLabel?.adjustsFontSizeToFitWidth = true
- return btn
- }()
-
- lazy var xBtn: UIButton = {
- let xBtn = UIButton.createButton(image: UIImage(named: "close_gray")) { [weak self] in
- guard let self = self else { return }
- self.isHidden = true
- }
- xBtn.isHidden = true
- return xBtn
- }()
-
- override func creatUI() {
- contentView.addSubview(blurEffect)
- blurEffect.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- }
- let imageViewTop = 225
- contentView.addSubview(imageView)
- imageView.snp.makeConstraints { make in
- make.width.height.equalTo(200.0)
- make.centerX.equalToSuperview()
- make.top.equalTo(imageViewTop)
- }
-
- contentView.addSubview(animatedImageView)
- animatedImageView.snp.makeConstraints { make in
- make.width.height.equalTo(200.0)
- make.centerX.equalToSuperview()
- make.top.equalTo(imageViewTop)
- }
-
- contentView.addSubview(cusStackView)
- cusStackView.snp.makeConstraints { make in
- make.top.equalTo(imageView.snp.bottom).offset(-20)
- make.leading.equalTo(0)
- make.trailing.equalTo(0)
- make.bottom.equalTo(0)
- }
-
- cusStackView.addSubviewToStackWhiteBoard(timeLabel)
- timeLabel.snp.makeConstraints { make in
- make.top.equalTo(0)
- make.leading.equalTo(16)
- make.trailing.equalTo(-16)
- make.height.equalTo(27)
- make.bottom.equalTo(-17)
- }
-
- cusStackView.addSubviewToStackWhiteBoard(textLabel)
- textLabel.snp.makeConstraints { make in
- make.top.equalTo(0)
- make.leading.equalTo(16)
- make.trailing.equalTo(-16)
- make.bottom.equalTo(-8)
- }
-
- cusStackView.addSubviewToStackWhiteBoard(infoLabel)
- infoLabel.snp.makeConstraints { make in
- make.top.equalTo(0)
- make.leading.equalTo(16)
- make.trailing.equalTo(-16)
- make.bottom.equalTo(-20)
- }
-
- cusStackView.addSubviewToStackWhiteBoard(backgroundGenerateBtn)
- backgroundGenerateBtn.snp.makeConstraints { make in
- make.top.equalTo(20)
- make.leading.equalTo(32)
- make.trailing.equalTo(-32)
- make.height.equalTo(48)
- make.bottom.equalTo(0)
- }
-
- cusStackView.addSubviewToStackWhiteBoard(regenerateBtn)
- regenerateBtn.snp.makeConstraints { make in
- make.top.equalTo(20)
- make.centerX.equalToSuperview()
- make.height.equalTo(44)
- make.width.equalTo(126*kDesignScale)
- make.bottom.equalTo(0)
- }
-
- //关闭按钮
- contentView.addSubview(xBtn)
- xBtn.snp.makeConstraints { make in
- make.top.equalTo(k_Height_StatusBar + 4)
- make.leading.equalTo(16)
- make.width.equalTo(36)
- make.height.equalTo(36)
- }
-
- timeLabel.superview?.isHidden = true
- infoLabel.superview?.isHidden = true
- setBackgroundGenerateBtnHidden(true)
-
- }
- 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
- }
-
- func setBackgroundColor(color:UIColor){
- blurEffect.removeFromSuperview()
- contentView.backgroundColor = color
- }
- }
- extension TSGeneratorloadingView{
-
- func updateShowProgress(text:String) {
- isHidden = false
- showLoading(text: text)
- isRotating = true
-
- if timeLabel.text?.count ?? 0 > 0 {
- timeLabel.superview?.isHidden = false
- }
-
- if infoLabel.text?.count ?? 0 > 0 {
- infoLabel.superview?.isHidden = false
- }
- }
-
-
- func updateShowBackstage(show:Bool){
- setBackgroundGenerateBtnHidden(!show)
- }
-
- func updateShowLoading(text:String){
- isHidden = false
- showLoading(text: text)
- isRotating = true
-
- if timeLabel.text?.count ?? 0 > 0 {
- timeLabel.superview?.isHidden = false
- }
-
- if infoLabel.text?.count ?? 0 > 0 {
- infoLabel.superview?.isHidden = false
- }
- }
-
- func updateShowError(text:String?){
- let msg = "Failed to generate, please try later".localized
- isHidden = false
- showError(text: msg)
- isRotating = false
- timeLabel.superview?.isHidden = true
- infoLabel.superview?.isHidden = true
- setBackgroundGenerateBtnHidden(true)
- }
-
- func updateShowSuccess(){
- isHidden = true
- isRotating = false
- setBackgroundGenerateBtnHidden(true)
- }
-
-
- func setBackgroundGenerateBtnHidden(_ isHidden:Bool){
- backgroundGenerateBtn.superview?.isHidden = isHidden
- }
- }
|