Swift自定义segue不适用于连续的segue

时间:2016-04-03 09:07:37

标签: ios swift segue

我制作了一个自定义segue,将当前视图向左移动,同时从右侧移动下一个视图。当我在第一个segueway上使用它时,它完美地工作,但是当在下一个segueway中使用时,只有目标视图移动。 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)
        }
    }
}

以下是视图控制器按钮操作方法中的代码,其中我启动了segue:

performSegueWithIdentifier("customSlideSegue", sender: self)

这是一段显示segues的视频:

The second slide happens after the image picker.

有人能看到问题吗?或者我该如何调试呢?感谢回复!

1 个答案:

答案 0 :(得分:0)

问题是第二次调用segue时,sourceviewcontroller的视图转换已经有-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)
    }
}