动画,同时改变视图迅速

时间:2016-09-13 10:35:52

标签: ios swift

我是swift的新手。我想在将视图从一个视图控制器更改为其他视图控制器时进行动画。我在按钮点击时使用 prepareForSegue:方法,我搜索了很多但是没有找到任何解决方案,有人有解决方案吗?

2 个答案:

答案 0 :(得分:1)

// 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.

答案 1 :(得分:0)

您不必使用prepareForSeque,您可以这样做:

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

viewcontrollerID在storyboard中设置(在属性检查器(右侧面板)中称为身份),它应该是唯一的,ViewControllerClass是ViewController的类

如果您正在使用UINavigationController

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