Animate NSLayoutConstraint.constant
We can wrap NSLayoutConstraint similarly to how we wrap views, and animate its constant property with signals.
I'd like to help with this. Can't be sure when I'll get to it, but if someone else has time before I do they can feel free to steal it.
Should this use the same RAN struct, or should it be separated into something like RANView and RANConstraint? The former would be get a little icky implementation-wise, but the latter feels like you're repeating something that can be inferred anyways.
@indragiek It should definitely use a different type but! we can cheat and change RAN into a free function that selects the right type. :trollface:
@jspahrsummers Nice :+1:
@jspahrsummers If I make RAN a free function to have it choose the correct type, how will accessing the properties work? RANView has alpha, bounds, etc. and RANConstraint would only have constant. Making both conform to a common protocol doesn't seem feasible.
@jspahrsummers Never mind, that was dumb. Forgot about function overloading.
I was making my own local copy to use in one of my projects and there's a problem with the current PR & animating constraints, mainly because those animations require that the constraint value be set outside of animateWithDuration. Furthermore you have to call view.layoutIfNeeded() in the animation block. That means the current implementation for constraints in the PR and branch doesn't animate.
The way I was thinking about doing is was:
- Add a view property to the constraints struct
- call
constraint.view?.layoutIfNeeded()in the binding
public struct ReactiveConstraint {
private weak var constraint: NSLayoutConstraint?
private weak var view: UIView?
private let willDealloc: SignalProducer<(), NoError>
public init(_ constraint: NSLayoutConstraint, view: UIView?) {
self.constraint = constraint
self.view = view
self.willDealloc = constraint.rac_willDeallocSignal()
.toSignalProducer()
.map { _ in () }
.flatMapError {
fatalError("rac_willDeallocSignal failed with error: \($0)")
()
}
}
}
public func <~ (constraint: ReactiveConstraint, signal: Signal<CGFloat, NoError>) -> Disposable {
let disposable = CompositeDisposable()
let constraintDisposable = constraint.willDealloc.startWithCompleted {
disposable.dispose()
}
disposable.addDisposable(constraintDisposable)
let signalDisposable = signal.observe(Observer(
next: {
constraint.constraint?.constant = $0
constraint.view?.layoutIfNeeded()
},
completed: {
disposable.dispose()
}
))
disposable.addDisposable(signalDisposable)
return disposable
}
Now my constraint animations work without having to change the animateEach method.