ios - UIStoryboardSegue sourceViewController 在 destinationViewController 之上

标签 ios swift storyboard segue

我想通过 segue 将我的 View Controller “滑出”到 destinationViewController 上方。这是我的代码:

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

    dst.view.superview?.insertSubview(src.view, aboveSubview: dst.view)
    src.view.transform = CGAffineTransformMakeTranslation(0, 0)

    UIView.animateWithDuration(0.25,
        delay: 0.0,
        options: UIViewAnimationOptions.CurveEaseInOut,
        animations: {
            src.view.transform = CGAffineTransformMakeTranslation(src.view.frame.size.width * -1, 0)
        },
        completion: { finished in
            src.presentViewController(dst, animated: false, completion: nil)
        }
    )
}

幻灯片看起来不错,但是在动画期间 destinationViewController 是全黑的,当动画完成时 destinationViewController 正确显示。

如何让它在 segue 动画期间显示?

编辑:

解决了:

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

    src.view.superview?.insertSubview(dst.view, belowSubview: src.view)
    dst.view.transform = CGAffineTransformMakeTranslation(0, 0)

    UIView.animateWithDuration(0.25,
        delay: 0.0,
        options: UIViewAnimationOptions.CurveEaseInOut,
        animations: {
            src.view.transform = CGAffineTransformMakeTranslation(src.view.frame.size.width * -1, 0)
        },
        completion: { finished in
            src.presentViewController(dst, animated: false, completion: nil)
        }
    )
}

最佳答案

试试这个:

class FirstCustomSegue: UIStoryboardSegue {

    override func perform() {
        // Assign the source and destination views to local variables.
        var firstVCView = self.sourceViewController.view as UIView!
        var secondVCView = self.destinationViewController.view as UIView!

        // Get the screen width and height.
        let screenWidth = UIScreen.mainScreen().bounds.size.width
        let screenHeight = UIScreen.mainScreen().bounds.size.height

        // Specify the initial position of the destination view.
        secondVCView.frame = CGRectMake(screenWidth, 0.0, screenWidth, screenHeight)

        // Access the app's key window and insert the destination view above the current (source) one.
        let window = UIApplication.sharedApplication().keyWindow
        window?.insertSubview(secondVCView, aboveSubview: firstVCView)


        // Animate the transition.
        UIView.animateWithDuration(0.4, animations: { () -> Void in
            firstVCView.frame = CGRectOffset(firstVCView.frame, -screenWidth, -0.0)
            secondVCView.frame = CGRectOffset(secondVCView.frame, -screenWidth, -0.0)

            }) { (Finished) -> Void in
                self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController,
                    animated: false,
                    completion: nil)
        }
    }

}

关于ios - UIStoryboardSegue sourceViewController 在 destinationViewController 之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32693611/

相关文章:

ios - 在 Google Maps SDK 中查找要路线的位置距离

ios - 下载前检查 NSURL 的资源是否存在

ios - 自动布局问题 : Prevents Image from loading?

ios - 获取 UITableViewCellAccessoryDe​​tailButton 的框架(为 nil)

ios - 为一副纸牌中的每个 Card 实例赋值

ios - UISearchController searchBar 在第一次点击时消失

swift - 使用 Swift 将可编码结构保存到 UserDefaults

swift - Xcode 7.3.1 更新至 8,带有 cocoapods 的 Swift2 项目即将构建

xcode - 如何防止 Storyboard 调整(拉伸(stretch))以 UIView 为中心的 UIImage 的大小?

ios - 我们是否需要为所有 iPhone/iPad 设备提供单独的 Storyboard?