jo solution
jo solution

Reputation: 411

How can I Zoom in/out Animation when present UIViewController in Swift 4?

I want to present ViewController from button Click with Zoom-In/Out Animation, I try some code but it won't worked for me:

func btnClickView(_ sender: UIButton) {
   let objImagePreview = ImagePreview(nibName: "ImagePreview", bundle: nil)
            let transition = CATransition()
            transition.duration = 1
            transition.type = kCATransitionMoveIn
            transition.subtype = kCATransitionFromTop
            transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
            view.window!.layer.add(transition, forKey: kCATransition)
            self.present(objImagePreview, animated: false, completion: nil)
}

I want to animate UIViewController not UIView . I tried so many things but it won't worked for me.

Upvotes: 2

Views: 4751

Answers (1)

Rian Erlangga
Rian Erlangga

Reputation: 99

I hope this will be help,

in the first view controller set destination ViewController with vc.modalPresentationStyle = .overCurrentContext before present it, and set animated as false while present like this

if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DestinationViewController") as? DestinationViewController {
            vc.modalPresentationStyle = .overCurrentContext
            self.present(vc, animated: false, completion: nil)
        }

and then in the DestinationViewController when viewWillAppear create zoom animation like this


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        view.transform = CGAffineTransform(scaleX: 0.00001, y: 0.00001)
        UIView.animate(withDuration: 0.5, animations: { [weak self] in
            self?.view.transform = CGAffineTransform.identity
        })
    }

for me this code works

an example of the results of gif using the code above

Upvotes: 5

Related Questions