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!
Related
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
In my app, there is a scrollView(pageScrollView) which is a snapchat like interface (https://www.youtube.com/watch?v=1_daE3IL_1s)
However, when I try to use below code to get the gesture on the scroll view, it cannot detect, can anyone help me on this??
let leftSwipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(leftSwipedByUser(_:)))
leftSwipeGesture.direction = .left
self.pageScrollView.addGestureRecognizer(leftSwipeGesture)
let rightSwipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(rightSwipedByUser(_:)))
rightSwipeGesture.direction = .right
self.pageScrollView.addGestureRecognizer(rightSwipeGesture)
And the action is below:
#objc func leftSwipedByUser(_ gesture:UISwipeGestureRecognizer) {
print("left swipe")
}
#objc func rightSwipedByUser(_ gesture:UISwipeGestureRecognizer) {
print("right swipe")
}
So what I'm trying to do is have each cell in my tableview have the ability to be swiped left or right. If I can grab the finger location/translation I could change the content's x anchors and bingo bango, but I'm having a real hard time figuring it out. if anyone can help, it would be so so rad
You can check the apple documentation about UISwipeGestureRecognizer , The default direction is right, also explore this function UISwipeGestureRecognizerDirection it return you the direction is in right, left, top , or bottom . Here is demonstration
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
}
}
}
i am new to swift programming. So i was trying to add swipe gestures to my application for iOS. But when i try to swipe it stops app and says NSException caught. Here is the code, i have written.
override func viewDidLoad() {
super.viewDidLoad()
textLabels.append(label0)
textLabels.append(label1)
textLabels.append(label2)
textLabels.append(label3)
textLabels.append(label4)
textLabels.append(label5)
textLabels.append(label6)
textLabels.append(label7)
textLabels.append(label8)
textLabels.append(label9)
textLabels.append(label10)
textLabels.append(label11)
textLabels.append(label12)
textLabels.append(label13)
textLabels.append(label14)
textLabels.append(label15)
setupInitial()
print(textLabels.count)
var request = GADRequest()
request.testDevices = [kGADSimulatorID]
bannerView.adUnitID = "ca-app-pub-8950283126375215/1694851281"
bannerView.rootViewController = self
bannerView.load(request)
createAndLoadInterstitial()
let swipeRight = UISwipeGestureRecognizer(target: self, action: Selector(("gesture:")))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeDown = UISwipeGestureRecognizer(target: self, action: Selector(("gesture:")))
swipeDown.direction = UISwipeGestureRecognizerDirection.down
self.view.addGestureRecognizer(swipeDown)
//setView(view: hideView, hidden: false)
}
func gesture(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
}
}
}
I also tried adding UIGestureRecognizerDelegate to my viewController class like this
class ViewController: UIViewController, UIGestureRecognizerDelegate
but it did not work. I also added UIGestureRecognizer. Please help me, i am really stuck here. I am using Xcode 8 and OSX El Capitan. Thanks in advance.
The stringly-typed Selector() has been deprecated for the new #selector().
Update your action to #selector(gesture(gesture:)) and that should fix it
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(gesture(gesture:)))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
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: