【问题标题】:Lottie animation subview of UIRefreshControl appears off-centerUIRefreshControl 的 Lottie 动画子视图出现偏离中心
【发布时间】:2021-08-17 17:05:11
【问题描述】:

我目前正在使用方形 Lottie 动画实现自定义 UIRefreshControl。我一直在努力解决 Lottie 子视图对我的 UIRefreshControl 的自动布局约束。我真的很感激任何建议。 我正在尝试使用以下代码使我导入的 (json) 动画的大小和位置与标准的普通 Apple UIRefreshControl 相同:

    lazy private var refreshControl: UIRefreshControl = {
    let rf = UIRefreshControl()
    rf.tintColor = .clear
    
    let loadingAnimation = Animation.named("My_Animation")
    let lottieView = AnimationView(animation: loadingAnimation)
    lottieView.play()
    lottieView.loopMode = .loop
    
    lottieView.contentMode = .scaleAspectFit
    lottieView.frame = rf.bounds      
    rf.addSubview(lottieView)

    return rf
}()

我的问题是这会导致我的动画在 UIRefreshControl 父视图中居中而不是居中。我尝试使用以下约束:

lottieView.centerXAnchor.constraint(equalTo: rf.centerXAnchor).isActive = true
        lottieView.bottomAnchor.constraint(equalTo: rf.bottomAnchor).isActive = true
    lottieView.trailingAnchor.constraint(equalTo: rf.trailingAnchor).isActive = true
    lottieView.leadingAnchor.constraint(equalTo: rf.leadingAnchor).isActive = true

在其他教程和帖子中,我一直看到有人建议关闭 translatesAutoresizingMaskIntoConstraints,但是当我这样做时,动画会比屏幕大,并且大部分时间都在屏幕外。

【问题讨论】:

    标签: ios swift autolayout lottie uirefreshcontrol


    【解决方案1】:

    对于其他为此苦苦挣扎的人,我将 UIRefreshControl 子类化并在 override init() 中调用了以下约束。似乎已经解决了这个问题:

        func setupLayout() {
        translatesAutoresizingMaskIntoConstraints = false
        animationView.frame = CGRect(x: UIScreen.main.bounds.midX - 160, y: 0, width: 320, height: 60)
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-21
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      • 2021-09-26
      相关资源
      最近更新 更多