淡出动画Swift

时间:2016-07-01 05:43:10

标签: ios swift animation

如何从一个View控制器到下一个控制器制作淡入淡出动画。所以基本上我想要的是当我点击一个按钮时,它从当前视图控制器淡化到它链接到的那个。有没有办法做到这一点。我一直在寻找一段时间,并且无法在Swift中找到任何东西。谢谢你的帮助!

2 个答案:

答案 0 :(得分:5)

let transition: CATransition = CATransition()
    transition.duration = 0.4
    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = kCATransitionFade
    self.navigationController!.view.layer.addAnimation(transition, forKey: nil)

    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("vcID") as! My_ViewController
    self.navigationController?.pushViewController(vc, animated: false)

答案 1 :(得分:0)

这是我的答案。我没有让每个场景成为不同的ViewController,而是让每个场景成为一个SKScene,并为整个项目使用了一个ViewController。现在,为了从一个场景到另一个场景我使用了这个:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch: AnyObject in touches {
        // Get the location of the touch in this scene
        let location = touch.location(in: self)
        // Check if the location of the touch is within the button's bound

        if L1.contains(location) {
            let gameScene = GameScene()
            gameScene.scaleMode = .resizeFill
            gameScene.view?.ignoresSiblingOrder = true
            let transition = SKTransition.fade(withDuration: 1)
            self.scene?.view?.presentScene(gameScene, transition: transition)
            print("tapped!")
        }
}

这允许我使用&#34; self.scene?.view?.presentScene(gameScene,transition:transition&#34;来实现这种转换。我从视图控制器转移的原因不仅仅是为了动画,但也使应用程序更有效,让它更好地工作,更顺畅。