ios - Swift 自定义转场不适用于连续转场

标签 ios swift segue

我制作了一个自定义segue,将当前 View 向左移动,并同时从右侧移动下一个 View 。当我在第一个 Segueway 上使用它时,它工作得很好,但是当在下一个 Segueway 中使用时,只有目标 View 移动。 Segue 的代码如下所示:

class CustomSlideSegue: UIStoryboardSegue {

    override func perform() {
        let firstVCView = self.sourceViewController.view as UIView!
        let secondVCView = self.destinationViewController.view as UIView!

        let screenWidth = UIScreen.mainScreen().bounds.size.width
        let screenHeight = UIScreen.mainScreen().bounds.size.height

        secondVCView.frame = CGRectMake(screenWidth, 0, screenWidth, screenHeight)

        let window = UIApplication.sharedApplication().keyWindow
        window?.insertSubview(secondVCView, aboveSubview: firstVCView)

        UIView.animateWithDuration(0.6, animations: { () -> Void in
             firstVCView.transform = CGAffineTransformMakeTranslation(-screenWidth, 0)
             secondVCView.transform = CGAffineTransformMakeTranslation(-screenWidth, 0)
    }) { (Finished) -> Void in
            self.sourceViewController.presentViewController(self.destinationViewController as UIViewController, animated: false, completion: nil)
        }
    }
}

这是 View Controller 按钮操作方法中的代码,我在其中开始转场:

performSegueWithIdentifier("customSlideSegue", sender: self)

这是一个显示转场的视频:

The second slide happens after the image picker.

谁能看出问题所在吗?或者我应该如何调试这个?感谢您的回复!

最佳答案

问题是,第二次调用 segue 时,sourceviewcontroller 的 View 变换已经具有 -screenWidth 值,因此它不会被推出屏幕。将您的代码更改为以下内容:

override func perform() {
    let firstVCView = self.sourceViewController.view as UIView!
    let secondVCView = self.destinationViewController.view as UIView!

    let screenWidth = UIScreen.mainScreen().bounds.size.width

    secondVCView.transform = CGAffineTransformMakeTranslation(screenWidth, 0)

    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(secondVCView, aboveSubview: firstVCView)

    UIView.animateWithDuration(0.6, animations: { () -> Void in
        firstVCView.transform = CGAffineTransformMakeTranslation(-screenWidth, 0)
        secondVCView.transform = CGAffineTransformIdentity
    }) { (Finished) -> Void in
        self.sourceViewController.presentViewController(self.destinationViewController, animated: false, completion: nil)
    }
}

关于ios - Swift 自定义转场不适用于连续转场,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36383553/

相关文章:

Swift:调用时出现 Segue 错误

ios - 如何将对象转至 UITabBarController

ios - 来自 AVCaptureSession 的 UIImage

ios - 即使设备处于横向,也可以纵向启动应用程序

ios - 哪种数据模型最适合存储 Instagram 之类的数据?

arrays - 数组创建导致 'Type of expression is ambiguous without more context'

swift - 当 ubiquitousItemDownloadingStatusKey 为 'Current' 时,我正在尝试运行一个函数

ios - prepareForSegue 是在 View Controller 之间传递值的正确方法吗

ios - Xcode 6 中的 UITabbar

ios - 调用setNeedsDisplay并不总是触发drawLayer :inContext: