swipe recognizer get only one direction recognized - swift

swipegesturerecognizer recognize only the right direction.
left, up or down did not recognized and not even calls the selector
am using swift 5 Xcode 10.3
func setupUserInteraction(){
self.view.isUserInteractionEnabled = true
self.view.addGestureRecognizer(UISwipeGestureRecognizer(target: self, action: #selector(handelSwipe)))
}
#objc func handelSwipe(g: UISwipeGestureRecognizer){
if g.direction == .right {
print("Swipe Right")
}
if g.direction == .left {
print("Swipe Left")
}
}
this code above shows "swipe right" when i swipe right
nothing when swipe left

thanks alot
it works by adding two swipe gesture recognizers
i defined the direction by each then attach them to the view
each have separated selector handler
func setupUserInteraction(){
self.view.isUserInteractionEnabled = true
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeRight))
swipeRight.direction = .right
view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeLeft))
swipeLeft.direction = .left
view.addGestureRecognizer(swipeLeft)
}
#objc func handleSwipeRight(){
print("Swipe Right")
}
#objc func handleSwipeLeft(){
print("Swipe Left")
}

Adding onto #matt, you can only have one direction on a swipe recognizer, while the default is 'right'. You can/should set the direction manually.
Depending on your use case, you should also look into UIPanGestureRecognizers as it would support multidirectional flows, but requires more setup.

Related

swift4 UISwipeGestureRecognizer Viewcontroller with large title and cells not working

I have a ViewController with large titles. In my ViewController are 2 cells. UISwipeGestureRecognizer is not working by swiping down. I want to call a searchController with this swipe-action in the titleView. Could the reason be the large title ?( with swipe down the title goes down and becomes greater, thats OK ). But my swipe action will not be recognized. Any ideas?
my code: - function called in viewDidLoad() in ViewController
func swipeSetup() {
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(funcSwipedDown(sender:)))
swipeDown.direction = .down
self.view.addGestureRecognizer(swipeDown)
}
#objc func funcSwipedDown(sender: UISwipeGestureRecognizer){
Utils.displayAlert(title: "Info", message: "swipe down")
self.addNavigationbar() // func for adding search bar
}

Playing a sound with Swipe Gesture on ViewController in Swift 4

I need to know, how I can add a wav sound (page flipping) to a viewController when a Swipe Gesture is activated. I have 5 different View Contrllers, and when the child swipes the gesture, I would like it to make play a file sound (page flipping). I already implemented the Swipe Gestures on all viewControllers (LEFT & RIGHT), but need the sound to go along with the Gesture.
How I would do it. Add the gesture recognizer to the view in code and setup an action for it to call with each swipe. Get the direction in the function in case you want different sounds or behavior.
override func viewDidLoad() {
super.viewDidLoad()
var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
var swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.view.addGestureRecognizer(swipeLeft)
}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
// Play sound here
case UISwipeGestureRecognizerDirection.Left:
// Play sound here
default:
break
}
}
}

Display menu list on Double Tap gesture - Swift3

I'm new to IOS and I'm trying to display list of buttons on double tap of UIView. I have the below code to capture the double tap gesture:
let menuTap = UITapGestureRecognizer(target: self, action: #selector(showMenuPanel(_:)))
menuTap.minimumPressDuration = 0.0
menuTap.numberOfTapsRequired = 2
menuTap.delaysTouchesBegan = true
self.view.isUserInteractionEnabled = true
self.view!.addGestureRecognizer(menuTap)
func showMenuPanel(_ recognizer: UITapGestureRecognizer) {
print("TESTPANEL")
}
I need to design a panel to show a list of buttons such as start, stop and pause. Can anybody guide me on how to design a panel on the tapped position?
You need to use UITapGestureRecognizer for that not the UILongPressGestureRecognizer. Set UITapGestureRecognizer with numberOfTapsRequired to 2.
let menuTap = UITapGestureRecognizer(target: self, action: #selector(showMenuPanel(_:)))
menuTap.numberOfTapsRequired = 2
//No need to set isUserInteractionEnabled to true because by default it is true for `UIView`
//self.view.isUserInteractionEnabled = true
self.view!.addGestureRecognizer(menuTap)
Add action method of tapGesture like this.
func showMenuPanel(_ recognizer: UITapGestureRecognizer) {
print("TESTPANEL")
let point = recognizer.location(in: self.view)
//Get your view from nib
let view = CustomView()
//set its origin to this point
view.frame.origin = point
//add your view in self.view
self.view.addSubview(view)
}

About UITapGestrueRecognizer in Swift

I have made a view subclass of UIView.Then, I added UITapGestureRecognizer to it but when I tap on the view, it don't respond to it.
Code:
let tapGesture = UITapGestureRecognizer(target: self, action: "singleTapHandler:")
self.maskView!.addGestureRecognizer(tapGesture)
selector:
func singleTapHandler(recognizer: UITapGestureRecognizer) {
print("xxx")
dismiss()
}
Ensure that you have enabled user interactivity:
self.maskView!.userInteractionEnabled = true
Make sure the maskView on top of the view, and the frame not equals to CGRectZero
Check if you added UIGestureRecognizerDelegate to your view, and use that tapGesture.delegate = self
Your tapGesture should be like let tapGesture = UITapGestureRecognizer(target: self, action: "singleTapHandler"), there is no need of :
Then your function can
func singleTapHandler(){
//action
}
Lastly, make sure you tap on the view you added the tap gesture.

How do I implement a UIGestureRecognizer in my app in Swift?

Lets say I have an image of a tree and when I tap on the screen it changes to an image of a car how would I do this? This is the code I have so far but its not working. (I have three images named "gp1", "gp2", and "gp3" and all of them have different number of taps to change the image.)
I'm in Spritekit and using Swift.
class GameScene: SKScene, SKPhysicsContactDelegate {
//Create Touch
createTouch()
}
override func didMoveToView(view: SKView) {
func createTouch() {
gestureRecognizer.numberOfTapsRequired = 1
gestureRecognizer2.numberOfTapsRequired = 2
gestureRecognizer3.numberOfTapsRequired = 3
gestureRecognizer == UITapGestureRecognizer(target: self, action: "gp2")
gestureRecognizer2 == UITapGestureRecognizer(target: self, action: "gp3")
gestureRecognizer3 == UITapGestureRecognizer(target: self, action: "gp1")
}
}
As Mundi said, for each UIImageView, make sure userInteractionEnabled = true. Otherwise, users won't be able to tap the image. Then, create the gestureRecognizer. Set the action to the method that will handle the gesture. Next, add the gestureRecognizer to the UIImageView. Finally, write the method to handle the gesture.
#IBOutlet var gp1: UIImageView!
#IBOutlet var gp2: UIImageView!
#IBOutlet var gp3: UIImageView!
func createTouch() {
// maker sure userInteractionEnabled is true
gp1.userInteractionEnabled = true;
gp2.userInteractionEnabled = true;
gp3.userInteractionEnabled = true;
// create the gesture recognizers
let gestureRecognizer1 = UITapGestureRecognizer(target: self, action: "tapGesture:")
let gestureRecognizer2 = UITapGestureRecognizer(target: self, action: "tapGesture:")
let gestureRecognizer3 = UITapGestureRecognizer(target: self, action: "tapGesture:")
// set the number of taps
gestureRecognizer1.numberOfTapsRequired = 1
gestureRecognizer2.numberOfTapsRequired = 2
gestureRecognizer3.numberOfTapsRequired = 3
// add the gesture recognizer to the UIImageViews
gp1.addGestureRecognizer(gestureRecognizer1)
gp2.addGestureRecognizer(gestureRecognizer2)
gp3.addGestureRecognizer(gestureRecognizer3)
}
Since these are all tap gestures, I've decided to use one method and a switch statement. You could, however, create different methods for each gesture if you wanted to.
func tapGesture(gesture: UITapGestureRecognizer) {
switch gesture.numberOfTapsRequired {
case 1:
gp1.image = UIImage(named: "car")
case 2:
gp2.image = UIImage(named: "car")
case 3:
gp3.image = UIImage(named: "car")
default:
break
}
}
You need to add the recognizer to the view and implement the action handlers gp2 etc.
For UIImageView you should also make sure userInteractionEnabled is set to true.