ReactiveAnimation icon indicating copy to clipboard operation
ReactiveAnimation copied to clipboard

Animate NSLayoutConstraint.constant

Open jspahrsummers opened this issue 9 years ago • 7 comments

We can wrap NSLayoutConstraint similarly to how we wrap views, and animate its constant property with signals.

jspahrsummers avatar Mar 23 '15 05:03 jspahrsummers

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.

indragiek avatar Mar 23 '15 05:03 indragiek

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 avatar Mar 23 '15 05:03 indragiek

@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 avatar Mar 23 '15 05:03 jspahrsummers

@jspahrsummers Nice :+1:

indragiek avatar Mar 23 '15 05:03 indragiek

@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.

indragiek avatar Mar 25 '15 15:03 indragiek

@jspahrsummers Never mind, that was dumb. Forgot about function overloading.

indragiek avatar Mar 25 '15 15:03 indragiek

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:

  1. Add a view property to the constraints struct
  2. 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.

thebarndog avatar Feb 13 '16 22:02 thebarndog