【问题标题】:Adding a subview inside a view which is a subview of Window在视图中添加子视图,该视图是 Window 的子视图
【发布时间】:2019-10-30 06:38:03
【问题描述】:

我正在尝试使用 Lottie 动画创建自定义 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
    })
}
}

【问题讨论】:

    标签: ios swift uiview lottie


    【解决方案1】:

    尝试像这样使用您的资源

    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
    }()
    

    因为您的方法每次访问都会生成一个新对象

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多