Andrew Harris Andrew Harris - 7 months ago 58
Swift Question

How to perform a segue with a swipe right gesture

I want to perform a segue by having a swipe right gesture.

showing a finger scrolling at the edge of the cell

What's the best practice?

Answer

I am realizing a segue with a "left to right swipe" with a custom segue and a swipe gesture recognizer.

You need to create a new .swift file and use the segue as class at the interface builder properties.

enter image description here

//  Created by David Seek on 25.04.16.
//  Copyright © 2016 David Seek. All rights reserved.
//

import UIKit

class fromLeftToRightSegue: UIStoryboardSegue {
    override func perform() {

        let firstVC = self.sourceViewController.view as UIView!
        let secondVC = self.destinationViewController.view as UIView!

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


        secondVC.frame = CGRectMake(-screenWidth, 0, screenWidth, screenHeight)

        let window = UIApplication.sharedApplication().keyWindow
        window?.insertSubview(secondVC, aboveSubview: firstVC)

        // Animate the transition.
        UIView.animateWithDuration(0.3, animations: { () -> Void in // set animation duration

            firstVC.frame = CGRectOffset(firstVC.frame, 0.0, 0.0) // old screen stay

            secondVC.frame = CGRectOffset(secondVC.frame, screenWidth, 0.0) // new screen strave from left to right

        }) { (Finished) -> Void in
            self.sourceViewController.presentViewController(self.destinationViewController as UIViewController,
                                                            animated: false,
                                                            completion: nil)
        }
    }

}

Swipe Gesture Recognizer also needs to be entered into a new .swift file:

import Foundation
import UIKit

//
//  UISwipeGestureRecognizer.h
//  UIKit
//
//  Copyright (c) 2009-2015 Apple Inc. All rights reserved.
//

// Recognizes: when numberOfTouchesRequired have moved mostly in the specified direction, enough to be considered a swipe.
//             a slow swipe requires high directional precision but a small distance
//             a fast swipe requires low directional precision but a large distance

// Touch Location Behaviors:
//     locationInView:         location where the swipe began. this is the centroid if more than one touch was involved
//     locationOfTouch:inView: location of a particular touch when the swipe began

public struct UISwipeGestureRecognizerDirection : OptionSetType {
    public init(rawValue: UInt)

    public static var Right: UISwipeGestureRecognizerDirection { get }
    public static var Left: UISwipeGestureRecognizerDirection { get }
    public static var Up: UISwipeGestureRecognizerDirection { get }
    public static var Down: UISwipeGestureRecognizerDirection { get }
}

@available(iOS 3.2, *)
public class UISwipeGestureRecognizer : UIGestureRecognizer {

    public var numberOfTouchesRequired: Int // default is 1. the number of fingers that must swipe
    public var direction: UISwipeGestureRecognizerDirection // default is UISwipeGestureRecognizerDirectionRight. the desired direction of the swipe. multiple directions may be specified if they will result in the same behavior (for example, UITableView swipe delete)
}

Called inside your desired ViewController.swift by:

private var swipeGestureRecognizer: UISwipeGestureRecognizer?

override func viewDidLoad() {
    super.viewDidLoad()
    swipeGestureRecognizer = MySwipeGestureRecognizer(target: self, swipeLeftSegue: "yourSwipeLeftSegue", swipeRightSeque: "yourSwipeRightSegue")
        view.addGestureRecognizer(swipeGestureRecognizer!)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "yourSwipeLeftSegue" {
        //your code
    }
}

Hope this works for you, if you have any questions, let me know.