i want to swipe right and left in swift - ios

i have a project in xcode 7 ( swift ) that i want to load differents viewscontrollers designeds in storyboard with the function swipe right and left and go to next viewcontroller or back.
Now i have this but only fade in right to left and i want the two fade ins.
func respondToSwipeGesture(sender: UISwipeGestureRecognizer) {
switch sender.direction {
case UISwipeGestureRecognizerDirection.Right:
print("SWIPED DERECHA")
self.performSegueWithIdentifier("cambio2", sender: nil)
case UISwipeGestureRecognizerDirection.Left:
print("SWIPED IZQUIERDA")
self.performSegueWithIdentifier("cambio", sender: nil)
default:
break
}
}

You can also use UISwipeGestureRecognizer by creating 2 instance of it.
one for each direction.
var swipeLeft : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipe:")
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
var swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipe:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeLeft)
self.view.addGestureRecognizer(swipeRight)
and the swipe function
func swipe(sender: UISwipeGestureRecognizer) {
switch sender.direction {
case UISwipeGestureRecognizerDirection.Right:
print("SWIPED DERECHA")
self.performSegueWithIdentifier("first", sender: nil)
case UISwipeGestureRecognizerDirection.Left:
print("SWIPED IZQUIERDA")
self.performSegueWithIdentifier("second", sender: nil)
default:
break
}
}

You're going about this all wrong. You need to look into interactive custom transitions. So, for example, if you want to swipe left and right to mean push and pop in a navigation controller, then implement the navigation controller's delegate's navigationController:animationControllerForOperation:fromViewController:toViewController: and navigationController:interactionControllerForAnimationController: and go from there.

UISwipeGestureRecognizer should only has one direction allowed, you should use UIPanGestureRecognizer.
let swipe = UIPanGestureRecognizer(target: self, action: "respondToSwipeGesture:")
self.view.addGestureRecognizer(swipe);
func respondToSwipeGesture(sender: UIPanGestureRecognizer) {
let point = sender.velocityInView(self.view)
//left or right
if point.x > 0 {
self.performSegueWithIdentifier("segue1", sender: nil)
}else{
self.performSegueWithIdentifier("segue2", sender: nil)
}
Of course both controllers should be in UINavigationController
For Storyboard settings:

Related

Swipe right to dismiss edge size

Is it possible to increase edge size interactivePopGestureRecognizer of UINavigationController reacts user touches?
I.e. is it possible to make swipe right from middle of screen to move and pop current top Viewcontroller same way as from the left screen edge?
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
}
// you can track all direction swipe like below, and to do that first you have to add gesture in the view you want to track.
#objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case .right:
// push pop here
print("Swiped right")
case .down:
print("Swiped down")
case .left:
// push pop here
print("Swiped left")
case .up:
print("Swiped up")
default:
break
}
}
}
Finally, found a solution to increase the edge touch size using this library: SwipeTransition

programmatically simulate a swipe gesture in Swift

I am implementing a gesture recognizer for swiping in Swift. I wan to be able to simulate the flinging of the card (programmatically swipe the view).
I assumed there would be a built in function for this but all I have found is one for tap gesture not swipe gesture.
This is how I am implementing the swipe gesturing:
let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
cardView.addGestureRecognizer(gesture)
cardView.userInteractionEnabled = true
}
func wasDragged (gesture: UIPanGestureRecognizer) {
let translation = gesture.translationInView(self.view)
let cardView = gesture.view!
// Move the object depending on the drag position
cardView.center = CGPoint(x: self.view.bounds.width / 2 + translation.x,
y: self.view.bounds.height / 2 + translation.y)
You can't simulate a gesture recognizer in its full implications (I mean, you can't actually make iOS think it's a real user action).
You can, however, fool your own code making it act as if it were a real swipe. For that, you need to create a gesture recognizer first:
var gestureRecognizerSwipeRight = UISwipeGestureRecognizer(target: self, action: "activatedGestureRecognizer:")
gestureRecognizerSwipeRight.direction = UISwipeGestureRecognizerDirection.Right
yourView.addGestureRecognizer(gestureRecognizerSwipeRight)
And then pass it directly to your action:
// Some other place in your code
self.activatedGestureRecognizer(gesture: gestureRecognizerSwipeRight)
Your activatedGestureRecognizer(gesture:) method should be something like:
func activatedGestureRecognizer(gesture: UIGestureRecognizer) {
if let gestureRecognizer = gesture as? UIGestureRecognizer {
// Here you can compare using if gestureRecognizer == gestureRecognizerSwipeRight
// ...or you could compare the direction of the gesture recognizer.
// It all depends on your implementation really.
if gestureRecognizer == gestureRecognizerSwipeRight {
// Swipe right detected
}
}
}
In fairness, I don't see any real gain in doing it this way. It should be a lot better to simply do the action associated with the swipe instead of actually simulating the gesture recognizer.
If you need, for instance, to animate your card while swipping, why don't you simply disable the user interaction on your card view and animate it programmatically?
Here is code you can use to programatically swipe the cell. When performed, the cell will animate to the swiped position.
#IBAction
func swipeFirstCell() {
if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) {
tableView.perform(Selector("_endSwipeToDeleteRowDidDelete:"), with: nil) // cancel any existing swipes
tableView.perform(Selector("_swipeToDeleteCell:"), with: cell)
}
}
Note you need to also return true for the cell from canEditRowAtIndexPath.
This might get flagged by Apple's private API detection but you can at least use it for development and perhaps obfuscate the string if you really to.
For SWIFT 3.0
let swipeRightOrange:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(slideToRightWithGestureRecognizer))
swipeRightOrange.direction = .Right;
let swipeLeftOrange:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(slideToLeftWithGestureRecognizer))
swipeLeftOrange.direction = .Left;
#IBAction func slideToLeftWithGestureRecognizer(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.blue
}
#IBAction func slideToRightWithGestureRecognizer(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.lightGray
}
You can create the UIPanGestureRecognizer by yourself and pass it to the wasDragged method. You should check with different values of the translation though:
let gesture = UIPanGestureRecognizer()
gesture.setTranslation(CGPointMake(0, 100), inView: self.view)
wasDragged(gesture)
SWIFT 4.2
let gesture = UIPanGestureRecognizer()
gesture.setTranslation(CGPoint(x: 0, y: 100), in: self.view)
wasDragged(gesture)
Although I asume you need something else. Why do you need to simulate this gesture in the first place?
You need to implement UISwipeGestureRecognizer
override func viewDidLoad() {
super.viewDidLoad()
var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeDown.direction = UISwipeGestureRecognizerDirection.Down
self.view.addGestureRecognizer(swipeDown)
}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
print("Swiped right")
case UISwipeGestureRecognizerDirection.Down:
print("Swiped down")
case UISwipeGestureRecognizerDirection.Left:
print("Swiped left")
case UISwipeGestureRecognizerDirection.Up:
print("Swiped up")
default:
break
}
}
}

UISwipeGestureRecognizer not working for swipe right

I'm working on an iOS app that's programmed in Swift and for some reason my app is unable to recognize/handle the swipe right gesture. Here is my code:
self.feedTableViewCell.productPreview.userInteractionEnabled = true
var swipeLeft = UISwipeGestureRecognizer(target: self, action: Selector("respondToSwipeGesture:"))
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.feedTableViewCell.productPreview.addGestureRecognizer(swipeLeft)
var swipeRight = UISwipeGestureRecognizer(target: self, action: Selector("respondToSwipeGesture:"))
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.feedTableViewCell.productPreview.addGestureRecognizer(swipeRight)
And here's my handler:
func respondToSwipeGesture(swipeGesture: UISwipeGestureRecognizer) {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Left:
println("swipe left")
case UISwipeGestureRecognizerDirection.Right:
println("swipe right")
default:
println("Unknown gesture")
break
}
self.feedTableViewCell.pageControl.currentPage = self.currentFeedImage
}
I'm able to swipe to the left fine, but when I swipe right, the app crashes and I get an (lldp) error. Any help greatly appreciated!

Change iOS view / view controller using swipe gesture

How can I implement a swipe gesture to change view to and fro?
The best example I've seen so far is the Soundcloud application but I couldn't figure out how to make it work.
Compatible with Swift 5
override func viewDidLoad()
{
super.viewDidLoad()
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
leftSwipe.direction = .left
rightSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
#objc func handleSwipes(_ sender: UISwipeGestureRecognizer)
{
if sender.direction == .left
{
print("Swipe left")
// show the view from the right side
}
if sender.direction == .right
{
print("Swipe right")
// show the view from the left side
}
}
Use this code...
override func viewDidLoad() {
super.viewDidLoad()
var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
println("Swiped right")
//change view controllers
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("StoryboardID") as ViewControllerName
self.presentViewController(resultViewController, animated:true, completion:nil)
default:
break
}
}
}
You can a UISwipeGestureRecognizer to your UIView and add to this gesture a target and an action to perform when the gesture occurs
var swipeGesture = UISwipeGestureRecognizer(target: self, action: "doSomething")
myView.addGestureRecognizer(swipeGesture)
func doSomething() {
// change your view's frame here if you want
}
This tutorial might be helpful to you: http://www.avocarrot.com/blog/implement-gesture-recognizers-swift/
Basically, you'll need to add a gesture recognizer to your view that listens for swipe gestures. Then when it detects a swipe, push to the next view.

Swift - performseguewithidentifier transition style from right to left

How can I make a transition from left to right using performseguewithidentifier? What I want to do is when user swipe to right, the page/view controller's transition will switch from right to left, normally it will swipe from left to right. Is this possible?
My code:
override func viewDidLoad() {
var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
performSegueWithIdentifier("LeftSegue", sender: self)
println("Swiped right")
default:
break
}
}
}
You have to write your own custom interactive transition.
Here and here you can find example tutorial. It's quite easy but have a little bit of boiler plate code.

Resources