简体   繁体   English

整个视图从左上角动画化

[英]Entire view is animating in from the top-left

Hey there internet reader,嘿那里的互联网阅读器,

I'm building a small feature where my banner animates in from the top using Snapkit and Combine.我正在构建一个小功能,我的横幅使用 Snapkit 和 Combine 从顶部动画。 In my viewDidLoad , I call setupObservers which creates this publisher below.在我的viewDidLoad中,我调用setupObservers在下面创建这个发布者。

Deeper in my app, isShown is toggled so that the publisher fires when the view is seen for the first time, causing not just the banner but the entire view to animate in from the top left, 0,0 position.在我的应用程序的更深处, isShown被切换,以便发布者在第一次看到视图时触发,不仅导致横幅而且整个视图从左上角开始动画,0,0 position。

I know this is because I'm calling self.view.layoutIfNeeded() , I'm just curious how to mitigate this so I can get the view to load in without animation, then show the banner.我知道这是因为我正在调用self.view.layoutIfNeeded() ,我只是好奇如何缓解这种情况,这样我就可以在没有 animation 的情况下加载视图,然后显示横幅。

Thanks!谢谢!

    public override func viewDidLoad() {
        super.viewDidLoad()

        setupObservers()

        viewStore.send(.viewDidLoad)
    }
        viewStore.publisher
            .banner
            .isShown
            .sink { isShown in
                guard self.initLoadFinished else { return }

                UIView.animate(withDuration: Metrics.animationDuration, delay: 0, options: .curveEaseInOut) {
                    self.banner.snp.updateConstraints {
                        if isShown {
                            self.topConstraint = $0.top.equalToSuperview()
                        } else {
                            self.topConstraint = $0.top.equalToSuperview().offset(-Metrics.bannerHeight)
                        }
                    }
                    self.view.layoutIfNeeded()
                }
            }.store(in: &cancellables)

Get setting the constant out of the animation block从 animation 块中设置常量

self.banner.snp.updateConstraints {
    if isShown {
         self.topConstraint.update(offset: 0)  
    } else {
         self.topConstraint.update(offset:-Metrics.bannerHeight)
    }
}  
UIView.animate(withDuration: Metrics.animationDuration, delay: 0, options: .curveEaseInOut) { 
    self.view.layoutIfNeeded() 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM