2017-02-04 10 views
0

我有這個從左到右的代碼,但我希望它做到從右到左。我會如何去做這件事?我會如何在相反的方向使這個segue?

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

    let firstVCView = sourceViewController.view 
    let secondVCView = destinationViewController.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.3, 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 , 
                 animated: false, 
                 completion: nil) 
    } 
+0

也許這可以幫助你http://stackoverflow.com/a/35574014/1622430 – carmine

+0

@carmine Mmm。看起來,這樣做的方式是將視圖控制器放在前面,而不是在Snapchat中滑動的位置。 –

回答

0

您需要做的是最初將第二個視圖放在第一個視圖左側而不是右側。

secondVCView.frame = CGRectMake(-screenWidth, 0.0, screenWidth, screenHeight) 

然後在你想在反向去動畫:

firstVCView.frame = CGRectOffset(firstVCView.frame, screenWidth, 0.0) 
secondVCView.frame = CGRectOffset(secondVCView.frame, screenWidth, 0.0) 

所以最終的結果:

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

let firstVCView = sourceViewController.view 
let secondVCView = destinationViewController.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.3, 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 , 
                animated: false, 
                completion: nil) 
} 
相關問題