UISwipeGesture not registering first swipe - swift

Here's my code in viewDidLoad:
//add a swipe gesture
let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(rotateDial(_:)))
downSwipe.direction = .down
self.view.addGestureRecognizer(downSwipe)
The first time I swipe down it doesn't recognize the gesture. I read in another SO that the swipe gesture is registering itself on the first swipe. How do I fix that?

Related

How to if double tap else single tap UITapGestureRecognizer

I am trying to recognise if users tap a single tap once or double-tap
if (gestureRecognizer.numberOfTouchesRequired == 2 ){
print("Double")
}else{print("single")
}
Thank you for you advice
you will need to use two gesture recognizers for this . One to capture the single tap gestures and another for double taps .
Sample code :
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didPressPartButton))
singleTapGesture.numberOfTapsRequired = 1
view.addGestureRecognizer(singleTapGesture)
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap))
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)

How to add Swipe Gesture on a view that has a Pan Gesture as well?

So I have a view with a carousel that has two views in it. I have two buttons that navigate from the two views inside the carousel. When the user is on the right view, the right button is disabled and left is enabled. When the user is on the left view, the left button is disabled and right is enabled.
However, I have a Pan gesture that allows the user to also swipe in between the two pages without using the buttons.
When I use the gesture to move in between the two views, I also need the buttons to reflect the change (either be enabled or disabled), however, when I put it on my pan gesture function it doesn't work 100% of the time as a half swipe or short swipe not strong enough to shift the pages but is strong enough to disable/enable my buttons as if the view has changed even though it hasn't.
I tried incorporating a Handle Swipe but I couldn't get the function to work.
Can anyone take a look at this code and let me know if anything is missing? Or better yet, if there is an easier solution to accomplish my goal
#objc func yesswiped (recognizer: UISwipeGestureRecognizer){
print("swipe pls")
}
override func viewDidLoad() {
super.viewDidLoad()
setNavigationBar()
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(yesswiped))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(yesswiped))
leftSwipe.direction = .left
rightSwipe.direction = .right
self.view.addGestureRecognizer(leftSwipe)
self.view.addGestureRecognizer(rightSwipe)
}

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
}
}
}

Gesture recognizer issues

I'm having trouble wrapping my brain around gesture recognizers. I want to target a subview. Here's an example that works with the main view:
let gesture = UITapGestureRecognizer(target: hex_pin_view!, action: #selector(openEmojis(sender:)))
gesture.delegate = self
mapView.addGestureRecognizer(gesture)
Here's the subview target, this dose not work and is what I'm looking for:
let gesture = UITapGestureRecognizer(target: hex_pin_view!.add_emoji_img_view, action: #selector(openEmojis(sender:)))
gesture.delegate = self
mapView.addGestureRecognizer(gesture)
I'm getting a crash error:
unrecognized selector sent to instance
Tho the selector is the same, I don't get it.
Anyone have a clue what I could be doing wrong?
The target of a gesture recogniser is the object that will receive messages from the gesture recogniser. or simply put, the object that holds the function that will handle the message.
Alot of the time the target: is set to self.
selector is the function/method that will handle the messages.
let gesture = UITapGestureRecognizer(target: self, action: #selector(openEmojis(sender:)))
gesture.delegate = self
mapView.addGestureRecognizer(gesture)
So there, the currrent class/object should have a method called openEmojis. The gesture is added to the mapView here
mapView.addGestureRecognizer(gesture)
So when the map gets tapped, your function should be called.
If you want the gesture to trigger when your subview is tapped. instead add the gesture recogniser to the subview instead:
add_emoji_img_view.addGestureRecognizer(gesture)

Tap Gesture isn't working. User interaction enabled.

Have following view structure:
Programmatically adding tap gesture rec to Temp lbl:
let tempLblTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MainFeedVC.convertDegrees))
tempLblTap.delegate = self
tempLblTap.numberOfTapsRequired = 1
tempLblTap.numberOfTouchesRequired = 1
tempLblTap.cancelsTouchesInView = false
self.tempLbl.isUserInteractionEnabled = true
self.tempLbl.addGestureRecognizer(tempLblTap)
but the method convertDegrees isn't triggered.
There are also 2 swipe gesture recognizers that are added to the same view:
let leftSwipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(MainFeedVC.showPostPicVC))
leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(leftSwipeGestureRecognizer)
let rightSwipeGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(MainFeedVC.showUserVC))
rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(rightSwipeGestureRecognizer)
Maybe they are the reason?
Found solution:
View that contained my lbl had its userInteractionEnabled unchecked in storyboards.
So, when adding gesture recognizers to labels, images or simply adding buttons, always check that all parent views has its userInteractionEnabled to true.