我正在尝试第一次构建自定义segue。所需的结果是在前一个后面创建新的ViewController,然后使之前的一个幻灯片像是从屏幕弹出/删除一样。我的问题是我无法做到这一点。如果我在前一个下面创建一个新的(在这种情况下两个都向上滑动,一个弹出另一个插入),这是可以的,但这不是我想要做的。我设法做的唯一事情是单独滑动前一个,但它下方有黑色背景而不是新视图......
这是我的代码(两者都在滚动那个,这不是预期的结果):
override func perform() {
let viewControllerToRemove = self.sourceViewController.view as UIView!
let viewControllerToShow = self.destinationViewController.view as UIView!
let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
viewControllerToShow.frame = CGRectMake(0.0, screenHeight, screenWidth, screenHeight)
let window = UIApplication.sharedApplication().keyWindow
window?.insertSubview(viewControllerToShow, aboveSubview: viewControllerToRemove)
UIView.animateWithDuration(0.6, animations: { () -> Void in
viewControllerToShow.frame = CGRectOffset(viewControllerToShow.frame, 0.0, -screenHeight)
viewControllerToRemove.frame = CGRectOffset(viewControllerToRemove.frame, 0.0, -screenHeight)
}) { (Finished) -> Void in
self.sourceViewController.presentViewController(self.destinationViewController as UIViewController,
animated: false,
completion: nil)
}
}
欢迎任何帮助, 提前谢谢!
答案 0 :(得分:3)
您应该使用转换管理器,请参阅以下代理: UIViewControllerAnimatedTransitioning和UIViewControllerTransitioningDelegate
对于你的动画,如果有一个全局容器会更容易,因为在你的动画中只有一个视图是动画的。
使用转换管理器,两个视图都位于转换容器中,因此您可以将destinationView作为可见背景。
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
// get reference to our fromView, toView and the container view that we should perform the transition in
let container = transitionContext.containerView()
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
然后设置框架以使两个视图在同一个地方,并执行以下操作:
container.addSubview(toView)
container.addSubview(fromView)
container.sendSubviewToBack(toView)
当您从theView设置动画时,此时您将在后台看到toView。
此链接的更多信息:http://mathewsanders.com/animated-transitions-in-swift/