2

I'm trying to animate a view's alpha property after I've sent it some values via a ReactiveSwift Signal Producer.

The following is how I am currently doing it sans animations.

// Somewhere in View Model (for all code below)
let shouldShowShutter = MutableProperty<Bool>(false)

// In my View
self.shutterButton.reactive.alpha <~ self.viewModel.shouldShowShutter.map({ (show) -> CGFloat in
        return show ? 1:0.0
    })

I can inelegantly animate the view via:

self.viewModel.shouldShowShutter.producer.startWithSignal { (observer, disposable) in
            observer.map({ (show) -> CGFloat in
                return show ? 1:0.0
            }).observeValues({ [unowned self] (alpha) in
                UIView.animate(withDuration: 0.5, animations: { 
                    self.shutterButton.alpha = alpha
                })
            })
        }

But I should be able to animate the views with ReactiveAnimation:

self.shutterButton.reactive.alpha <~ self.viewModel.shouldShowShutter.map({ (show) -> CGFloat in
        return show ? 1:0.0
    }).animateEach(duration: 0.2).join(.Concat)

The question: ReactiveCocoaLayout and ReactiveAnimation both don't seem to work anymore as of the time I am asking the question, at least not with the Swift 3 or ReactiveCocoa 5 as they depend a lot on the legacy RACSignals.

Is there a more elegant way to animate signal streams to ReactiveCocoa components with ReactiveCocoa 5?

Nate Lee
  • 2,842
  • 1
  • 24
  • 30

1 Answers1

2

I didn't know about ReactiveAnimation before, but I really liked the approach.

So I updated it for RAC 5.0 / Swift 3.2, have a look at my fork. I'll also create a pull request, lets have a look if we can get the project back to life.

I've included a small iOS Demo project to demonstrate the use:

label.reactive.center <~ SignalProducer.timer(interval: .seconds(1), on: QueueScheduler.main)
  .map { _ in return self.randomPoint() }
  // In order to demonstrate different flatten strategies,
  // the animation duration is larger than the animation interval,
  // thus a new animation begins before the running animation is finished
  .animateEach(duration: 1.5, curve: .EaseInOut)
  // With the .concat flatten strategy, each animations are concatenated.
  // Each animation finisheds, before the next one starts.
  // This also means, that animations are queued
  .flatten(.concat)
  // With the .merge flatten strategy, each animation is performed immediately
  // If an animation is currently running, it is cancelled and the next animation starts from the current animation state
  //.flatten(.merge)
MeXx
  • 3,357
  • 24
  • 39
  • Did you happen to add the demo eventually or? I'd be happy to look through and see what we could possibly do to restart it all. – Nate Lee Jun 20 '17 at 02:33
  • Ooops, I did add it as a submodule. Fixed it now, its directly in the repo: https://github.com/iv-mexx/ReactiveAnimation – MeXx Jun 20 '17 at 08:17