How can I include multiple UITextFields in the Hidekeyboard function given below? - ios

I am trying to implement a function that can let decimal pads disappear when the user clicks somewhere else but the keyboard. The issue is that I currently only know this function which to my knowledge only works for 1 textField:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.Alkoholanteil.endEditing(true)
}
I could only enter the text field "Referencetime" as you could see.
Is there a function that allows for the keyboards to disappear when the user is touching else but the keyboard for 4 text fields and not 1?

You can do it with:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
Which will work for all textField.

Related

touchesMoved touches.first!.view remains the same, if I move a finger out of view

I have a problem with touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) function. More specifically - touches.first!.view property.
I need to know the tag of view that user is pointing at. But when I drag finger out of view bounds, touches.first!.view value does not change.
This is what I am doing:
I have this code to print out selected view's tag and background color:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
print("\(touches.first!.view!.backgroundColor), View tag: " + String(touches.first!.view!.tag))
}
But I expect to get printed out is UIExtendedGrayColorSpace 1 1, View tag: 0, but instead of this I get <UIDynamicSystemColor: 0x600000820b40; name = systemGroupedBackgroundColor>, View tag: 5. This means, that even if I drag my cursor from one view, I still get the first clicked view from touches.first!.view.
How can I get the current selected view?
Answer based on Rob's comment above
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
if let touch = touches.first {
if ( !self.grayUIView.frame.contains(touch.location(in: self.grayUIView)) ){
return
}else {
//still in current view - process with your logic
}
}
}

How to pass touch event to other views only when touchtype is finger?

I have a subview over my view and i want this subview only to recognize pencil touch types. Every time the touch type is a finger i want it to be recognized by the parent view. Currently i am using the function shown below. The problem here is that the UIEvent has no touches so its not possible to check if it is a finger or pencil touch.
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
return false
}
Update
One option would be disabling user interaction on the bottom view, and in the top view implementing:
func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent)
func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent)
func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent)
In these functions, you can check the type with touches.first?.type, and you can check the view with touches.first?.view, and if they satisfy your criteria you can call touchesBegan/Moved/Ended on the bottom view.
What it said before:
Use these three methods:
func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent)
func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent)
func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent)
You can check they type with touches.first?.type, and you can check the view with touches.first?.view.
You could forward the touches by calling touchesBegan/Moved/Ended on the view underneath.

Close iOS Keyboard by touching outside ( a smarter way)

I have UIViewController with UITextfield and several other elements (several subviews and buttons). I want to close the keyboard when tapped somewhere outside of the UITextfield.
I know there is an answer Close iOS Keyboard by touching anywhere using Swift, but this way doesn't work if user taps on viewController's subviews or buttons.
I can add the similar UITapGestureRecognizer for each subViews and buttons, but is there a smarter way to resolve this?
extension UIViewController{
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
}
Also if you have elements in a UIScrollView, add this method too:
extension UIScrollView {
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.next?.touchesBegan(touches, with: event)
}
}

Getting the number of fingers touching a UIView

I want to change the color of a UIView based on the number of touches currently on it. I have managed to create this with a single touch:
class colorChangerViewClass: UIView {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
setColor(color: .blue)
print("touchesBegan")
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
setColor(color: .green)
print("touchesEnded")
}
func setColor(color: UIColor) {
self.backgroundColor = color
}
}
But I am struggling with implementing multitouch. I feel like I'm not understanding something here.
Does UIView have a property to check how many fingers are currently touching it?
I could check that each time a new touchesBegan or touchesEnded occurs.
First enable multi touch on your subclass
self.isMultipleTouchEnabled = true
Then inside your touch event functions you can get all touches with event from your UIView class.
let touchCount = event?.touches(for: self)?.count
Make sure the UIView has isMultipleTouchEnabled = true, the default is false.
Then change the touchesBegan method
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
setColor(color: .blue)
let touchCount = event?.touches(for: self)?.count
}

How to hide keyboard by touch in the screen if all my main view is cover by another views?

I cant touch in my main view, I have few other views on him.
this func :
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
dose'nt help me..
is there any func?
If you know which View is focused you can call resignFirstResponder() on it

Resources