maitree solanki
maitree solanki

Reputation: 135

animation while changing view swift

I am new to swift.I want to do animation while changing view from one view-controller to other view-controller.i am using prepareForSegue: method on button click, i have searched lot but did not find any solution do anyone have solution for this?

Upvotes: 2

Views: 1139

Answers (2)

Harshal.y
Harshal.y

Reputation: 26

// call following code in the method where you want to add animation effect.

func buttonClicked(sender: UIButton){

  let transition = CATransition()
  transition.duration = 0.5
  transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
  transition.type = kCATransitionPush
  transition.subtype = kCATransitionFromRight
  self.navigationController?.view.layer.addAnimation(transition, forKey: nil)
  self.navigationController?.pushViewController(vc!, animated: true)

}

// you can vary the duration of transition according to your convenience 

// here transition.type are types of animation which you want to add on view. now we are pushing view so used push type. if want, you can google it for more types.

// subtype will decide the direction on animation.

Upvotes: 1

JuicyFruit
JuicyFruit

Reputation: 2678

You do not have to use prepareForSeque, you can do like this:

@IBAction func buttonClicked(sender: UIButton) {   
    let destinationVC = storyboard?.instantiateViewControllerWithIdentifier("viewcontrollerID") as! ViewControllerClass
    self.presentViewController(destinationVC, animated: true, completion: nil)
}

viewcontrollerID is set in storyboard (it is called identity in attribute inspector (right panel)), it should be unique, ViewControllerClass is class of your ViewController

If you are using UINavigationController

@IBAction func buttonClicked(sender: UIButton) {
   let destinationVC = storyboard?.instantiateViewControllerWithIdentifier("viewcontrollerID") as! ViewControllerClass 
   self.navigationController?.pushViewController(destinationVC, animated: true)
}

Upvotes: 0

Related Questions