textFieldShouldReturn func not working on scrollView - swift

How could i make the text disappeared when the user tap the screen when UIscrollview is active
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
I'm using the typical func for get and dismiss the keyboard
I try to
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
UIScrollView.endEditing(true)
}

This in viewdidLoad ()
let scrollViewTap = UITapGestureRecognizer(target: self, action: #selector(self.scrollViewTapped))
scrollViewTap.numberOfTapsRequired = 1
scroll_view.addGestureRecognizer(scrollViewTap)
and This in class
#objc func scrollViewTapped() {
scroll_view.endEditing(true)
self.view.endEditing(true) // anyone
}

in ViewDidLoad
let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.touch))
recognizer.numberOfTapsRequired = 1
recognizer.numberOfTouchesRequired = 1
elskrolll.addGestureRecognizer(recognizer)
elskroll must be your UIScrollView name:
outside the ViewDidLoad
#objc func touch() {
//print("Touches")
self.view.endEditing(true)
}

Related

Tap background to dismiss TextField keyboard only if keyboard is active

I keep having an issue with a textfield I have set as a decimal keyboard, because its the decimal keyboard it doesn't have a return key so I have to tap background to dismiss it. I want to only allow it to recognize that tap if the keyboard is open. Currently it recognizes it anytime you tap the background. Any help would be awesome thanks.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
label.text = "tapped"
}
You can add this extension, it worked for me:
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
#objc func dismissKeyboard() {
view.endEditing(true)
}
}
Then put this self.hideKeyboardWhenTappedAround() in viewDidLoad
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}

How to use a tochesBegan() method as an extension of a class?

How can I add a touchesBegan() method to my class?
Here is my method:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let locationUser = touch.location(in: self)
}}
You can use class extension adding the method in it if your
class is a subclass of UIView or UIWindow:
extension YourClass {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
for touch in touches{
let locationUser = touch.location(in: self)
}
print("touchesBegan")
}
}

Touchesmoved - updating multiple elements on single drag

I'm trying to update multiple images upon clicking and dragging over the elements. I've implemented touchesbegan, touchesmoved, and touchesended, but I don't know how to make touchesmoved effect multiple images.
I've been scouring the web, but I haven't been able to find any guides on this. If you could point me in the right direction, or provide me with some basic advice, that would be greatly appreciated.
The following is an example image:
Edit: The following is an example image of what should be possible:
What it should look like.
I'd like to be able to effect the other letters in the same way through the same press, changing their pictures. These pictures are temporary images.
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("touches began:\(touches)")
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.image = UIImage(named: "slot")!
print("touches moved:\(touches)")
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.image = UIImage(named: "tile")!
print("touches ended")
}
If I understood you correctly, that should be something like this:
class ChangableView: UIView {
private var touchedImageView: UIImageView? {
didSet {
if oldValue == touchedImageView { return }
if let oldValue = oldValue {
oldValue.image = UIImage(named: "tile")!
}
if let newValue = touchedImageView {
newValue.image = UIImage(named: "slot")!
}
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard let touch = touches.first else { return }
updateTouchedImageViewWithTouch(touch, event: event)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard let touch = touches.first else { return }
updateTouchedImageViewWithTouch(touch, event: event)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
touchedImageView = nil
}
}
private extension ChangableView {
func updateTouchedImageViewWithTouch(touch: UITouch, event: UIEvent?) {
let touchPoint = touch.locationInView(self)
let touchedView = self.hitTest(touchPoint, withEvent: event)
touchedImageView = touchedView as? UIImageView
}
}
In addition your UIViewController should have as view a subclass of ChangableView and do not forget to set userInteractionEnabled property to all the UIImageViews to YES.

Why does double tap gesture only work once on UITextView?

I have a textview that I want to allow the user to edit when they double tap. The double tap works fine when the viewcontroller first loads. After I edit the textview and close the textview then reopens for editing with just a single tap. My code is:
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var textView: UITextView!
let myTaps = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textView.gestureRecognizers = nil
myTaps.addTarget(self, action: "handleTaps:")
myTaps.numberOfTapsRequired = 2
myTaps.numberOfTouchesRequired = 1
textView.addGestureRecognizer(myTaps)
}
func handleTaps(recognizer: UITapGestureRecognizer) {
print("Taps")
textView.becomeFirstResponder()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
view.endEditing(true)
super.touchesBegan(touches as Set<UITouch>, withEvent: event)
}
func textViewShouldEndEditing(textView: UITextView) -> Bool {
textView.endEditing(true)
return true
}
}

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

Hey so I keep getting this every time i try running the code to close the keyboard every other part works until i add these lines of code.
If anyone can help that would be great thank you!
Code starts here
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
self.view.endEditing(true)
//closes keyboard
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
self.view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField.text != nil
{
textField.resignFirstResponder()
}
you can also try
if textField != nil
{
textField.resignFirstResponder()
}
return true
}
}
Try this