How do I make this segue animation exit to the right or left? - Fast

This code works great to make my custom segue transition with reduced animation. How would I make the animation look like it is flowing left or right?

Also, I am using UISwipeGestureRecognizer to trigger the segue. It works great except that the segue happens as soon as I do the fewest hits. How can I let the segue not appear until the user throws their finger away, or at least does more swipe. Should I use scrollView for this?

Below is my code for my UISwipeGestureRecognizer.

override func viewDidLoad() {

    var swipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "showSecondViewController")
    swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Up
    self.view.addGestureRecognizer(swipeGestureRecognizer)

}    

      

The code below is my animation code

class FirstCustomSegue: UIStoryboardSegue {

override func perform() {

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

    // Access the app 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.5, animations: { () -> Void in
        firstVCView.frame = CGRectOffset(firstVCView.frame, 0.0, -screenHeight)
        secondVCView.frame = CGRectOffset(secondVCView.frame, 0.0, -screenHeight)

        }) { (Finished) -> Void in

                    self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController,
                        animated: false,
                        completion: nil)
            }
    }
}

      

+3


source to share


1 answer


Animation direction

You just need to change the source and destination frames. Currently, the start frame is secondVCView

X, Y origin 0.0

, screenHeight

which means it is just below the bottom of the screen. If you want it to appear on the right, you would need to change it to screenWidth

, 0.0

.

Then the target frame firstVCView

have changed, so that the beginning has been -screenWidth

, 0.0

and the target frame secondVCView

have changed, so that the beginning was 0.0

,0.0

override func perform() {

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

    // Access the app 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.5, 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)
            }
    }
}

      



Delayed napkins

Modify your method showSecondViewController

to accept a parameter UISwipeGestureRecognizer

and check its property state

. If you want to invoke a session when the user lifts their finger trigger, select state

UIGestureRecognizerStateEnded

.

If you want finer grained control, such as when they hit a certain distance, you will need to check the status for UIGestureRecognizerStateChanged

and monitor the location of the touch. I'll leave this as an exercise for the reader as there are many examples of quick google searches :)

+6


source







All Articles