嘿网上读取器,
我正在构建一个小功能,我的横幅动画从顶部使用Snapkit和组合。在我的viewDidLoad
中,我调用了setupObservers
,它在下面创建了这个发布者。
在我的应用程序中,isShown
会被切换,这样当第一次看到视图时,发布者就会触发,这不仅会导致横幅,还会导致整个视图从左上角0,0位置移动。
我知道这是因为我调用了self.view.layoutIfNeeded()
,我只是好奇如何减轻这个问题,这样我就可以在没有动画的情况下加载视图,然后显示横幅。
谢谢!
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)
发布于 2021-04-01 07:06:55
从动画块中获取常量设置
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()
}
https://stackoverflow.com/questions/66907000
复制