marcosgriselli / ViewAnimator

ViewAnimator brings your UI to life with just one line

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support of timing functions

Narayane opened this issue · comments

Hi,

Would it be possible to support timing functions like https://github.com/cats-oss/Sica#easingfunctions in ViewAnimator?

Thanks for this library.

@Narayane I was thinking about that feature some time ago. We currently support springs but supporting timing functions shouldn't be much work. Do you want to take a shot at it? I can help out with the PR.

Potentially. I'll keep you in touch :)

Hi @marcosgriselli,

I've tried to follow this approach but it doesn't work with your extension UIView.animate()...

Even when I add parameter options: [.curveEaseInOut] to your extension UIView.animate(), it doesn't seem to be taken into account at runtime: move is the save with and without it...

let fromAnimation = AnimationType.from(direction: .bottom, offset: 270)
UIView.animate(views: [self.myView], animations: [fromAnimation], delay: 0, duration: 1, options: [.curveEaseInOut])

Do you have any idea on how to make it?

Thanks.

@Narayane the reason why the options don't work on that particular function is that the library is using the UIView springs animation API. We need to create a new function that uses the UIView animation API that doesn't use springs.

Here's what the function will look like:

 static func animate(views: [UIView],
                    animations: [Animation],
                    reversed: Bool = false,
                    initialAlpha: CGFloat = 0.0,
                    finalAlpha: CGFloat = 1.0,
                    delay: Double = 0,
                    animationInterval: TimeInterval = 0.05,
                    duration: TimeInterval = ViewAnimatorConfig.duration,
                    options: UIView.AnimationOptions = [],
                    completion: (() -> Void)? = nil) {
    
    guard views.count > 0 else {
        completion?()
        return
    }
    
    views.forEach { $0.alpha = initialAlpha }
    let dispatchGroup = DispatchGroup()
    for _ in 1...views.count { dispatchGroup.enter() }
    DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
        for (index, view) in views.enumerated() {
            view.alpha = initialAlpha
            view.animate(animations: animations,
                         reversed: reversed,
                         initialAlpha: initialAlpha,
                         finalAlpha: finalAlpha,
                         delay: Double(index) * animationInterval,
                         duration: duration,
                         options: options,
                         completion: { dispatchGroup.leave() })
        }
    }
    
    dispatchGroup.notify(queue: .main) {
        completion?()
    }
}

I'll create a release later this week with support for this but for the moment that function should be enough to drop-in and use in your project.