1

I'm trying to follow the example in this project to create an expand/shrink view controller transition. In my case, the view controller which is being presented will have the same background color as the button itself, therefore this animation is essentially just the button expanding to cover the whole screen without ever disappearing.

enter image description here

https://github.com/AladinWay/TransitionButton

I've placed my button on the bottom right of the screen and managed to get it to expand using the following function:

private func expand(completion:(()->Void)?, revertDelay: TimeInterval) {

    let expandAnim = CABasicAnimation(keyPath: "transform.scale")
    let expandScale = (UIScreen.main.bounds.size.height/self.frame.size.height)*2
    expandAnim.fromValue            = 1.0
    expandAnim.toValue              = max(expandScale,26.0)
    expandAnim.timingFunction       = expandCurve
    expandAnim.duration             = 0.3
    expandAnim.fillMode             = .forwards
    expandAnim.isRemovedOnCompletion  = false

    CATransaction.setCompletionBlock {
        completion?()
        // We return to original state after a delay to give opportunity to custom transition
        DispatchQueue.main.asyncAfter(deadline: .now() + revertDelay) {
            self.setOriginalState(completion: nil)
            self.layer.removeAllAnimations() // make sure we remove all animation
        }
    }

    layer.add(expandAnim, forKey: expandAnim.keyPath)

    CATransaction.commit()
}

Now I'm trying to create the animation for dismissing the view controller which would essentially reverse the initial animation and shrink the whole screen back to the circular button on the bottom of the screen. I'm not sure how to adapt the above code to reverse this though.

Alk
  • 5,215
  • 8
  • 47
  • 116

1 Answers1

0

You could try using the exact same block, except switch the fromValue and toValue.

expandAnim.fromValue            = max(expandScale,26.0)
expandAnim.toValue              = 1.0
jake
  • 1,226
  • 8
  • 14