Ios Swift Override double tap of UItextField - ios

I want to write a custom code on double tap on UITextField and block the default text editing and popup of the keyboard. I've tried the following and nothing has worked for me so far. Kindly help me solve this problem.
let gestureArray = NamTxtBoxVal.gestureRecognizers
var tapGesture = UITapGestureRecognizer()
for idxVar in gestureArray!
{
if let tapVar = idxVar as? UITapGestureRecognizer
{
if tapVar.numberOfTapsRequired == 2
{
tapGesture = tapVar
NamTxtBoxVal.removeGestureRecognizer(tapGesture)
}
}
}
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(namFnc(_:)))
doubleTap.numberOfTapsRequired = 2
doubleTap.delegate = self
tapGesture.requireGestureRecognizerToFail(doubleTap)
NamTxtBoxVal.addGestureRecognizer(doubleTap)
I've also tried:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
return false
}

The only way I know of for you to do this is to
put a UIView behind the UITextField
set the textField's userInteractionEnabled = false
add the double tap gesture to the UIView
This will allow you to register a double tap on the TextField area and not popup any keyboard or enter editing mode.
Not sure what you plan on doing with the textField after double tap but you should be able to handle most stuff programmatically at this point.
Code for this is:
class ViewController: UIViewController {
#IBOutlet weak var myViewBehindMyTextField: UIView!
#IBOutlet weak var myTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.myTextFieldTapped(_:)))
tapGesture.numberOfTapsRequired = 2
myViewBehindMyTextField.addGestureRecognizer(tapGesture)
}
func myTextFieldTapped(sender: UITapGestureRecognizer) {
print("Double tapped on textField")
}
}

Related

Th Keyboard does not show up when I press a button once, but when I press a button for a long time

I add a click on uitextfield, the keyboard does not show when touched only with a long press in the field.
What can I do to make the keyboard pop up when I press it and I won't hold it for a long time?
TAP TAP works and deletes other views as well ...
What am I doing wrong?
class DeparturesView: UIViewController {
…
private let searchTextFieldData: UISearchTextField = {
let searchTextFieldData = UISearchTextField()
searchTextFieldData.translatesAutoresizingMaskIntoConstraints = false
searchTextFieldData.keyboardType = .alphabet
searchTextFieldData.borderStyle = .roundedRect
searchTextFieldData.autocorrectionType = .no
searchTextFieldData.spellCheckingType = .no
searchTextFieldData.layer.cornerRadius = 10
searchTextFieldData.backgroundColor = .white
searchTextFieldData.returnKeyType = .search
return searchTextFieldData
}()
}
override func viewDidLoad() {
…
//Mark - Add gesture to app
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return touch.view == self.view
}
//Mark - Hide keyboard
let hideKeyboards = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:)))
hideKeyboards.delegate = self
view.addGestureRecognizer(hideKeyboards)
//Mark - Detele other view and show keyboard
let hideTableView = UITapGestureRecognizer(target: self, action: #selector(tapHandler))
hideTableView.numberOfTapsRequired = 1
hideTableView.delegate = self
searchTextFieldData.addGestureRecognizer(hideTableView)
}
#objc func tapHandler(_ sender: UITapGestureRecognizer){
print("Show tap tap")
favouriteLabel.removeFromSuperview()
tableViewWithFavouriteBusStop.removeFromSuperview()
}
extension DeparturesView: UITextFieldDelegate{
func textFieldDidBeginEditing(_ textField: UITextField) {
becomeFirstResponder()
}
Follow staps
Remove "hideTableView" TapGesture
add self.earchTextFieldData.delegate = self in viewDidLoad and
replace "textFieldDidBeginEditing"
func textFieldDidBeginEditing(_ textField: UITextField) {
favouriteLabel.removeFromSuperview()
tableViewWithFavouriteBusStop.removeFromSuperview()
becomeFirstResponder()
}

Swift UIGestureRecognizer is not working / is not doing anything

I have a small issue that I couldn't fix for a few hours now! I have a simple ViewController with a label, which is linked to my ViewController class. I also set up an UIGestureRecognizer to change the text of the label when the label is clicked. But weirdly nothing happens at all.
The text of the label changes from whatever is set in the Storyboard to "Hello", so the setup is correct. But when I click the label, nothing happens.
Here's the entire ViewController class:
import UIKit
class PremiumViewController: UIViewController {
// Views
#IBOutlet weak var premiumLabel: UILabel!
// View Did Load
override func viewDidLoad() {
super.viewDidLoad()
premiumLabel.text = "Hello"
// Tap listener
let tap = UIGestureRecognizer(target: self, action: #selector(PremiumViewController.clicked))
premiumLabel.isUserInteractionEnabled = true
premiumLabel.addGestureRecognizer(tap)
}
#objc func clicked() {
premiumLabel.text = "You clicked me"
}
}
Use UITapGestureRecognizer instead of UIGestureRecognizer
change from
let tap = UIGestureRecognizer(target: self, action: #selector(PremiumViewController.clicked))
to
let tap = UITapGestureRecognizer(target: self, action: #selector(PremiumViewController.clicked))

Swift UIButton action and GestureRecognizer

I'm trying to solve this problem but I don't understand how to do it.
I have a view controller in which I have a view (CanvasView) and three buttons. Each button draw a type of shape. I want the user with a click of a button to add a shape with a tap in a certain point of the CanvasView only when the button is clicked.
Is it possible to allow tapGesture only when the button is clicked?
Here is the code:
#IBOutlet weak var CanvasView: CanvasView!
override func viewDidLoad() {
super.viewDidLoad()
let tapGR = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
tapGR.numberOfTapsRequired = 2
CanvasView.isUserInteractionEnabled = true
CanvasView.addGestureRecognizer(tapGR)
}
#IBAction func tap(_ sender: UITapGestureRecognizer) {
let tapPoint = sender.location(in: CanvasView)
let shapeView = ShapeSquare(origin: tapPoint)
CanvasView.addSubview(shapeView)
}
#IBAction func DrawSquare(_ sender: UIButton) {
CanvasView.setNeedsDisplay()
}
Keep the gesture disabled until the button is tapped.
Start by making the gesture a property:
var tapGR: UITapGestureRecognizer!
Then update viewDidLoad:
tapGR = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
tapGR.isEnabled = false
Then in your button handler:
tapGR.isEnabled = true

tap on UITextField not working

I read a lot try to find a way to handle uitextfield click but nothing working for me
What I want:
I want add tap on UITextField and open dialog
What I tried:
class CreateMessagesViewController: UIViewController {
#IBOutlet weak var testField: UITextField!
#IBOutlet weak var testView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
// testField.addGestureRecognizer(tap)
testView.addGestureRecognizer(tap)
testField.addTarget(self, action: #selector(onTapped(_:)), for: .touchDown)
}
func onTapped(_ sender : UITextField){
print("Hello World")
}
func handleTap(_ sender: UITapGestureRecognizer) {
print("Hello World")
}
}
As you can see I tried 2 ways:
UITapGestureRecognizer
selector
Just for the test I add simple UIView to make sure tap is working and it worked.
Why it doesn't work on uiTextField? Is there anything I have missed?
Instead of adding gesture recognizer you can write the code for opening dialog in
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
openDialog()//Function which will open the dialog
return false
}
I think it should have the same effect as you want.
1)add gesture recognizer:
let tapGR = UITapGestureRecognizer(target: self, action: #selector(someFunc))
tapGR.delegate = self
textField.addGestureRecognizer(tapGR)
2)make sure that you allow your tap handler when it is necessary:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true //or add your custom check here if you need
}
Without of the second part you get a buggy textField where sometimes tap gesture doesn't work, sometimes default gestures not handled!
There is a simpler way to achieve this. You should conform your view controller class to UITextFieldDelegate and implement textFieldDidBeginEditingin your class.
func textFieldDidBeginEditing(_ textField: UITextField){
//show popup here
}
If you are having multiple text fields, assign a tag to your text field in the storyboard(or in the code) to recognize the text field.
func textFieldDidBeginEditing(_ textField: UITextField){
// check your tag here
if textField.tag == 0 {
//show your popup here
}
}
Add GestureRecognizer to UITextField superview and check if help:
testField.superview.addGestureRecognizer(tap)
I created sample one for your question both Swift and Objective C but it not working.I used the Tap Gesture recognizer code for textField but it not even called.
Not Worked below Code in Swift and Objective C
Swift
let tap = UITapGestureRecognizer(target: self,action: #selector(handleTaponTextField(_:)))
tap.numberOfTapsRequired = 1
tap.delegate = self
txtFldTapMe.isUserInteractionEnabled = true
txtFldTapMe.addGestureRecognizer(tap)
func handleTaponTextField(_ sender: UITapGestureRecognizer)
{
print("Tap is handled here!")
}
Objective C
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.delegate = self;
txtfldTapME.userInteractionEnabled = YES;
[txtfldTapME addGestureRecognizer:tapGesture];
func handleTaponTextField(_ sender: UITapGestureRecognizer)
{
print("Tap is handled here!")
}
Then I tried with below addTarget action method for textField and it works fine.
Worked below Code in Swift and Objective C
Swift
txtFldTapMe.addTarget(self, action: #selector(textFieldEditDidBegin(_:)), for: .editingDidBegin)
func textFieldEditDidBegin(_ textField: UITextField) {
print("Text editing begin here!")
}
See the methods
You should use above methods of textField
Objective C
[txtfldTapME addTarget:self action:#selector(editingDidBeginStarted:) forControlEvents: UIControlEventEditingDidBegin];
-(void)editingDidBeginStarted:(UITapGestureRecognizer *)sender{
NSLog(#"Editing started here");
}
See the below textField methods
Try below code
yourTextField.addTarget(self, action: #selector(didChangeText), for: .editingChanged)
addSubview(view)
#objc func didChangeText(textField:UITextField) {
let str = textField.text
//your functionality here
}
It's not possible. Because Textfield delegate method call.

UIGestureRecognizer on only one part of the screen

If you look into the image, you'll see a textview, button and a slider.
I have completed all the steps except for the last one which is, when I tap on the slider, it constantly disappear. I know it disappear because I implemented UITapGestureRecognizer.
I guess my question is, I want the slider to disappear everytime I tap anywhere on the screen but when I am using the slider, I dont want the slider to disappear which is happening now every time I release my tap.
I have tried implementing one more UITapGestureRecognizer in sizeRefont with a function to keep sizeRefont.isHidden false but when I do that, the slider will not disappear whenever I tap on the screen.
I tried putting sizeRefont.isHidden = false in sizeRefont action and it doesnt work either.
class ResizeController: UIViewController {
#IBOutlet weak var sizeRefont: UISlider!
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissRefontSize(_:)))
view.addGestureRecognizer(tap)
}
#IBAction func sizeRefont(_ sender: AnyObject) {
let fontSize = CGFloat(sizeRefont.value)
textView.font = UIFont(name: textView.font!.fontName, size: fontSize * 30.0)
}
#IBAction func showSlider(_ sender: Any) {
sizeRefont.isHidden = false
}
func dismissRefontSize(_ sender: UITapGestureRecognizer) {
if sender.location(in: sizeRefont){
sizeRefont.isHidden = false
} else {
sizeRefont.isHidden = true
}
}
}
There is an error on if sender.location(in: sizeRefont) where it says CGPoint is not convertible to Bool
Image
First thing you need to do is you need to Adjust the dismissRefontSize() method to to the following :
func dismissRefontSize(_ sender: UITapGestureRecognizer) {
let location = sender.location(in: view)
If sizeReFont.frame.contains(location) {
// do nothing
}else {
sizeReFont.isHidden = true
}
}
The other thing you need to adjust is creating the tap recognized in your viewDidLoad() to the following:
let tap = UITapGestureRecognizer(target: self, action : #selector(dismissRefontSize(_:)))

Resources