与英雄的过渡不起作用

时间:2018-03-04 15:23:29

标签: ios swift cocoa-touch

我正在使用the Hero library在视图控制器之间进行转换。

第一个视图控制器:

class ViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    var people = [Person]()
    override func viewDidLoad() {
        super.viewDidLoad()
        hero.isEnabled = true
        hero.modalAnimationType = .push(direction: .right)

    }

    @IBAction func handleButton(){
        let view = self.storyboard?.instantiateViewController(withIdentifier: "secondVC") as! UIViewController
        present(view, animated: true, completion: nil)
    }
}

第二个视图控制器:

class Second: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        hero.isEnabled = true
        // Do any additional setup after loading the view.
    }
    @IBAction func handleBackButton(){
        hero.modalAnimationType = .push(direction: .left)
        hero.dismissViewController()
    }
}

它显示没有任何动画的新视图。当我关闭第二个控制器时,它将应用正确的动画。如何使用动画显示第二个视图?

2 个答案:

答案 0 :(得分:1)

好的找到了解决方案。 在第一种情况下,我有:

@IBAction func handleButton() {
  let view = self.storyboard?.instantiateViewController(withIdentifier: "secondVC") as! UIViewController
  present(view, animated: true, completion: nil)
}

我必须在视图上设置动画。

@IBAction func handleButton() {
  let view = self.storyboard?.instantiateViewController(withIdentifier: "secondVC") as! UIViewController
  view.hero.modalAnimationType = .push(direction: .right)
  present(view, animated: true, completion: nil)
}

作品

答案 1 :(得分:0)

通过设置Hero扩展名heroModalAnimationType

来设置模态动画类型
hero.heroModalAnimationType = .push(direction: .right)

请勿忘记使用Hero.shared对象的方法停用转换的默认动画:

func disableDefaultAnimationForNextTransition()
func setDefaultAnimationForNextTransition(_ animation: HeroDefaultAnimationType)