Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Animate NSLayoutConstraint.constant #4

Open
jspahrsummers opened this issue Mar 23, 2015 · 7 comments
Open

Animate NSLayoutConstraint.constant #4

jspahrsummers opened this issue Mar 23, 2015 · 7 comments

Comments

@jspahrsummers
Copy link
Member

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

@indragiek
Copy link
Member

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 indragiek self-assigned this Mar 23, 2015
@indragiek
Copy link
Member

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.

@jspahrsummers
Copy link
Member Author

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

@indragiek
Copy link
Member

@jspahrsummers Nice 👍

@indragiek
Copy link
Member

@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
Copy link
Member

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

@thebarndog
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants