是否可以创建从右到左过渡的自定义搜索

时间:2016-04-20 02:06:08

标签: ios swift segue

我正在使用Swift创建一个iOS应用程序,我想创建从右到左过渡的自定义segue。默认的转换方式是从左到右,但我确实需要采用相反的转换方式。我刚刚写了这样的代码。

class RightToLeftSegue: UIStoryboardSegue {

    override func perform() {
        let sourceViewController = self.sourceViewController 
        let destinationViewController = self.destinationViewController

        let window = UIApplication.sharedApplication().keyWindow
        window!.rootViewController = destinationViewController
        window!.rootViewController = sourceViewController
        UIView.transitionWithView(window!, duration: 0.5, options: .TransitionFlipFromLeft, animations: { window!.rootViewController = destinationViewController }, completion: { (finished: Bool) in })
    }
}

但这只是转换翻转动作。我能做到这一点吗?谢谢!

1 个答案:

答案 0 :(得分:2)

试试这个

class RightToLeftSegue : UIStoryboardSegue {

     override func perform() {

         guard let navigationController = sourceViewController.navigationController else { return }

         sourceViewController.view.superview?.addSubview(destinationViewController.view)

         let frame = sourceViewController.view.frame
         destinationViewController.view.frame = CGRectOffset(frame, -frame.size.width, 0.0)

         UIView.animateWithDuration(0.5,
         animations: {
             self.sourceViewController.view.frame = CGRectOffset(frame, frame.size.width, 0.0)
             self.destinationViewController.view.frame = frame
         })
         { (finished: Bool) in 
             navigationController.rootViewController = self.destinationViewController
         }

    }

}

修改

或者这个!

class RightToLeftSegue : UIStoryboardSegue {

    override func perform() {
        sourceViewController.navigationController?.viewControllers = [destinationViewController, sourceViewController]
        sourceViewController.navigationController?.popViewControllerAnimated(true)
    }

}