sCha
sCha

Reputation: 1504

UIViewControllerAnimatedTransitioning won't animate frame size

I'm trying to animate transitioning between UIViewControllers with Pop-in effect.

In my animateTransition(using transitionContext:) function:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    let containerView = transitionContext.containerView
    if let toController = transitionContext.viewController(forKey: .to) {
        let toView = toController.view!

        let finalFrame = transitionContext.finalFrame(for: toController) // (20, 20, 200, 200)
        containerView.addSubview(toView)

        toView.frame = CGRect(x: 20, y: 20, width: 100, height: 100)

        UIView.animate(withDuration: self.duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: .allowAnimatedContent, animations: {
            toView.frame = finalFrame
        }, completion: {(completed: Bool) -> Void in
            transitionContext.completeTransition(completed)
        })
    }
}

I was expecting that the size of the frame would change from 100 by 100 to 200 by 200. But it did not.

I found out that this function can animate the position of the frame while transitioning but not the size(width or height). How can I animate the size of the frame?

Upvotes: 1

Views: 709

Answers (2)

Andrea
Andrea

Reputation: 26385

This is due to fact that probably you are using auto layout in your app.
Once the layout engine starts it reposition the view based on your original constraints.
Try to modify your constraints (or apply a transform), not the frame. Using auto layout modifying the frame is almost useless.

Upvotes: 2

Paras Gorasiya
Paras Gorasiya

Reputation: 1351

You can try this instead of changing setting new frame with new size

UIView.animate(withDuration: self.duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: .allowAnimatedContent, animations: {
            toView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
        }, completion: {(completed: Bool) -> Void in
            transitionContext.completeTransition(completed)
        })

Upvotes: 1

Related Questions