【问题标题】:Transition with Hero not working与英雄的过渡不起作用
【发布时间】:2018-08-12 07:26:06
【问题描述】:

我正在使用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()
    }
}

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

【问题讨论】:

  • 安装Hero的pod命令是什么?我用过“英雄”吊舱。但是当我打开工作区下载后,它说不能在 xcode 9 中将 swift 2 转换为 3。

标签: ios swift cocoa-touch


【解决方案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)
}

作品

【讨论】:

    【解决方案2】:

    通过设置Hero扩展heroModalAnimationType来设置模态动画类型:

    hero.heroModalAnimationType = .push(direction: .right)
    

    不要忘记使用 Hero.shared 对象的方法禁用过渡的默认动画:

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

    【讨论】:

    • 它在两个视图控制器上都设置了。
    • 是的,我编辑了帖子以显示完整内容。如何使用 Hero.shared 禁用默认动画?
    • 嘿@Carpsen90,我在Hero.shared 上没有看到方法disableDefaultAnimationForNextTransition()...你能告诉我这是哪里吗?
    • PS - 我为 Swift 4 找到了 Hero.shared.defaultAnimation = .none
    猜你喜欢
    • 1970-01-01
    • 2015-03-05
    • 2020-05-15
    • 2021-06-23
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    相关资源
    最近更新 更多