Swift – Push UIView animation

Simple code snippet today that show how to push a UIView and obtain the same effect of a UINavigationController as push action, choosing also the side (.fromLeft / .fromRight).

We can do this using the CATransitionanimations. Very simple:

func push( view: UIView, side: CATransitionSubtype = .fromRight ) {
    let transition = CATransition()
    transition.type = CATransitionType.push
    transition.subtype = side

    self.layer.add(transition, forKey: nil)
    self.addSubview(view)
}

Enjoy pushing!

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.