2

I have a tabbed application with three tabs and I want to make the transition between them with a Swipe Gesture Recogniser.

  1. I dragged and dropped one Swipe Gesture Recogniser from the Object Library to the first View and one to the third View. I added two on the second View so I can swipe both left and right.
  2. I changed the Swipe to the Attribute Inspector so it will swipe right or left as needed
  3. Using the Assistant editor I used Control+Drag to create Actions to the proper ViewController.swift files.

What else should I do so the swiping should work? All of the guides I found online are in Objective-C. Does anyone know how to complete these Events in Swift?

Main Storyboard

FirstViewController.swift file

Fotios Tragopoulos
  • 449
  • 1
  • 7
  • 24
  • You should look into custom view controller transitions. They have been introduces in iOS8 and they can make this animated. And you should learn to read Objective-C. This language isn't going away soon and a lot of good examples are written in Objective-C. – dasdom Apr 22 '15 at 10:33
  • How about this article? http://www.ioscreator.com/tutorials/detecting-swipe-gesture-tutorial-ios8-swift – Access Denied Apr 22 '15 at 11:09

2 Answers2

0

You have to apply custom view controller transitions. Otherwise your tab transition will be neither animated (by default tab switches are without animation) nor interactive (you cannot control the speed with your gesture).

A good starting point is Colin Eberhardt's VC Transitions Library. It's in Objective-C but you can use it from Swift.

Just a warning: Custom view controller transitions are an immature part of iOS 7 or 8. If another view transition is initiated while a custom one is still running, the UI will more or less lock up. And it easily happens: user presses yet another tab before the transitions ends, your code switches tab and pushes a view controller at the same time, user manages to press a button during transition that opens another view controller etc.

Codo
  • 75,595
  • 17
  • 168
  • 206
0


EDIT: I have added a sample project to github and it fully works. Feel free to copy it or wok on it.



You don't need to add them in the story board. First add this code to your initializer (in most cases viewDidLoad):

    var swipeGesture = UISwipeGestureRecognizer(target: self, action: "SwipeToNextViewController:")
    swipeGesture.direction = UISwipeGestureRecognizerDirection.Left
    view.addGestureRecognizer(swipeGesture) 

    var swipeGestureReverse = UISwipeGestureRecognizer(target: self, action: "SwipeToPreviousViewController:")
    swipeGestureReverse.direction = UISwipeGestureRecognizerDirection.Right
    view.addGestureRecognizer(swipeGestureReverse)

Now that your viewController has the gesture, SwipeToNextViewController() will be called if you swipe left, and SwipeToPreviousViewController() will be called when swiped Right. Write the code for the two functions associated with the gestures:

func SwipeToNextViewController(gestureRecognizer:UISwipeGestureRecognizer)
{
    performSegueWithIdentifier("next", sender: self)
}

func SwipeToPreviousViewController(gestureRecognizer:UISwipeGestureRecognizer)
{
    performSegueWithIdentifier("previous", sender: self)
}

These two functions will be called when view has been swiped left or right. Add your code in it (in this case performSegue) and you can segue to another viewController.

Hope it helps :)

DanielEdrisian
  • 716
  • 1
  • 4
  • 17