swift4 UISwipeGestureRecognizer Viewcontroller with large title and cells not working - swift4

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
}

Related

adding a pan gesture to a subview of a UIView called from a UIViewController

I created a custom view with a subview where its frame origin x is set to the width of the parent view so that it looks like a tab sticking out of the view. (like a tab folder). The custom view is an IBOutlet in a view controller. My objective is to drag the tab to pull out the custom view like opening a panel. When I add the Pan gesture to the dragging tab, the subview, nothing happens. It works when I add it directly to the parent view. Any help would be awesome.
let pan = UIPanGestureRecognizer(target: self, action: #selector(dragView(pan:)))
pan.minimumNumberOfTouches = 1
pan.delegate = self
notesView.dragView.addGestureRecognizer(pan)
neither the func nor the delegate methods are triggered.
//MARK: - Don't forget to add UIGestureRecognizerDelegate, when you add delegate to gestureRecognizer
class ViewController: UIViewController, UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
//MARK: set panGestureRecognizer
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panGestureRecognizerTapped))
panGestureRecognizer.minimumNumberOfTouches = 1
//MARK: set panGestureRecognizer delegate
panGestureRecognizer.delegate = self
//MARK: Set panGestureRecognizer to view
view.addGestureRecognizer(panGestureRecognizer)
//MARK: - Add user interaction to you custom view or view
view.isUserInteractionEnabled = true
}
//MARK: when tap is happened the below attribute will be executed
#objc func panGestureRecognizerTapped() {
print("Pan Gesture Recognizer Tapped")
}
}
//The resource code is at the below link:
https://github.com/ahmetbostanciklioglu/PanGestureRecognizer.git

dismiss keyboard from all view hierarchy programmatically in swift

I have view controller like screenshot below.
When I touch textfield, the keyboard shows up.
I want to dismiss keyboard when I tap,drag on any screen.
I tried to put this code in viewcontroller, but doesn't work.
func dismissKey() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer( target: self, action: #selector(dismissKeyboard))
tap.cancelsTouchesInView = false
addGestureRecognizer(tap)
}
#objc func dismissKeyboard() {
endEditing(true)
resignFirstResponder()
}
but if I put this code in custom cell class type 1 ,it dismisses keyboard only when I tap on this cell.
how can I dismiss keyboard when I touch on any screen?
If you're trying to dismiss keyboard in UIViewController try:
#objc func dismissKeyboard() {
view.endEditing(true)
resignFirstResponder()
}

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

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.

Swift: Re-activating Tap Gesture on Disclosure Indicators

I have a Swift project that has a Table View Controller with multiple Static Cell Sections. Some cells have UITextFields and others have Accessory: Disclosure Indicators. I’ve implemented the following Swift code to dismiss the keyboard when the background is tapped:
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
tapGesture.cancelsTouchesInView = true
tableView.addGestureRecognizer(tapGesture)
}
func hideKeyboard() {
tableView.endEditing(true)
}
This works great for releasing the keyboard on tapping the background, but it also removed the tap gesture for the Disclosure Indicators (swipe still works). Does anyone know how to re-activate the tap gesture for the Disclosure Indicators cells after implementing this hideKeyboard() function?
Keep a reference to the gesture recognizer and remove it from the view when you hide the keyboard.
class YourController: UITableViewController {
let tapGesture: UITapGestureRecognizer = {
let tg = UITapGestureRecognizer(target: self, action: "hideKeyboard")
tg.cancelsTouchesInView = true
return tg
}()
override func viewDidLoad() {
super.viewDidLoad()
tableView.addGestureRecognizer(tapGesture)
}
func hideKeyboard() {
tableView.endEditing(true)
tableView.removeGestureRecognizer(tapGesture)
}
}