我有这段代码从左到右依次搜索,但我希望它从右到左执行。我将如何去做呢?

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)
    }

最佳答案

首先,需要将第二个视图放置在第一个视图的左侧而不是右侧。

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)
}

10-08 19:05