I met a very strange thing:
in storyboard has to ViewControllers : 1stVC and 2ndVC,
in 1stVC (Initial VC in storyboard) only have 2 functions:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//when touch view in 2ndVC,it will exec here!!!
super.touchesBegan(touches, with: event)
NSLog("xxx")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//switch to 2ndVC
performSegue(withIdentifier: "2ndVC", sender: nil)
}
Because app starts from 1stVC,but at viewDidAppear func it's soon to go to exec performSegue() func to switch to 2ndVC;now app should be in 2ndVC,but when I touch the screen (at 2ndVC's view), it will jump to the 1stVC's touchsBegin func (exec NSLog("xxx")).
How could that happen!? please help me,thanks!!!
Related
this probably has something to do with the fact that I am overriding touchesBegan in my custom subclass:
class myCustomButton: UIButton {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
print("TOUCHES BEGAN OVERRIDE CALLED")
originalColor = self.backgroundColor ?? .red
self.backgroundColor = .white
self.animateScale()
setNeedsDisplay()
}
//...
}
touchesEnded is similarly overridden, and both of these functions are called and print debug statements to console on button press. however, the code in the button pressed IBAction from my view controller is never run
#IBAction func nextButtonPressed(_ sender: Any) {
print("this is never executed")
//...
}
I have double checked that the IBOutlet is not broken and changed the (_ sender: Any) to (_ sender: AnyObject) both to no avail. any insights would be appreciated
ah ok, figured it out. in my custom button initializer I added a subview which, by default, had userInteractionEnabled set to true. setting userInteractionEnabled = true on my custom button object and userInteractionEnabled = false on my view before adding it as a subview allows my custom button to capture the touch and handle it properly rather than the view capturing the touch
Instead of overriding touchesBegan, try overriding the sendAction function of your UIButton:
override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
super.sendAction(action, to: target, for: event)
//Insert your code here
}
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)
}
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)
}
When I tap on my table view it will trigger touchesBegan and touchesShouldBegin. However, I also have a UIPanGestureRecognizer on my view cells and when panning a cell, tapping on other cells won't trigger touchesShouldBegin.
The reason I need touchesShouldBegin is because I want to stop the touches when a cell is being panned. Is there other methods I can use to do this? I have tried having set allowsMultipleSelection to false and true.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("This will always be printed")
}
override func touchesShouldBegin(_ touches: Set<UITouch>, with event: UIEvent?, in view: UIView) -> Bool {
print("This will only be printed when no cells are being panned")
return true
}
Try to set the UITableView's delaysContentTouches property to true.
You can also try to add the a UIGestureRecognizerDelegate to your UIPanGestureRecognizer and have :
extension MyViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
So I am wondering what func to use for closing a detailed ViewController opened from a tableView. Currently I have it working to close the view, but the touches event occurs when you touch anywhere on the screen, not with a specific button. I have tried just using the button on storyboards but no luck. Below is what I have working so far (again not button specific)...
//DetailedViewController.swift
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.dismissViewControllerAnimated(true, completion: nil)
}
Any and all feedback or direction is greatly appreciated!
#IBAction func buttonAction(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
Put the button in the storyboard view controller and right-click-drag from the little circle for this action to the button. Alternatively, create your button programmatically and call addTarget(self, action:"buttonAction:") on the button.