So I Wanted To Hide A UIView... Reactively

Life In Plastic, It’s Fantastic

Imagination, Life Is Your Creation

Come On Barbie, Let’s Go Party!

protocol MyViewModelInputs {
typealias ScrollInfo(contentOffsetY: CGFloat, topConstraintConstant: CGFloat, heightOfViewToMove: CGFloat)
func scrollViewDidScroll(info: ScrollInfo)
}
protocol MyViewModelOutputs {
var scrollViewSignal: Signal<CGFloat, NoError> { get }
}

Oh, I’m Having So Much Fun!

extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
viewModel.inputs.scrollViewDidScroll(info: (
contentOffsetY: scrollView.contentOffset.y,
topConstraintConstant: topConstraint.constant,
heightOfViewToMove: heightOfView
)
)
}
}
scrollViewSignal = scrollViewDidScroll.output
.combinePrevious()
.filter {
// .. is not at top, or bottom ...
// .. other conditions ...
}
.map { previous, current in
let minimumConstantValue = CGFloat(-current.heightOfViewToMove)
let delta = current.contentOffsetY - previous.contentOffsetY
if delta < 0 {
return min(current.topConstraintConstant - delta, 0)
} else {
return max(minimumConstantValue, current.topConstraintConstant - delta)
}
}

Well Barbie, We’re Just Getting Started

viewModel.outputs.scrollViewSignal.observeValues {
topConstraint.constant = $0
}

Post Mortem

If you liked this post, consider giving me a clap, or seven. このストーリーを好きだったら、拍手してくれませんか?

Aussie iOS Engineer based in Tokyo, Japan | 日英可