Search This Blog

Showing posts with label UITapGestureRecognizer. Show all posts
Showing posts with label UITapGestureRecognizer. Show all posts

Monday, October 30, 2017

iOS Swift. How to detect double OR triple tap with UITapGestureRecognizer

For detecting touches in iOS application we use UITapGestureRecognizer. When we need react only on special number of touches, for example two or three, we must simply specify this parameter for gesture recognizer object.

When number of touches is 3
let tapGestureRecognizer = UITapGestureRecognizer(target: self,
                                                          action: #selector(imageTappedTriple))
tapGestureRecognizer.numberOfTapsRequired = 3

When number of touches is 2
let tapGestureRecognizer = UITapGestureRecognizer(target: self,
                                                          action: #selector(imageTappedDouble))
tapGestureRecognizer.numberOfTapsRequired = 2

Important to remember is make enable image for interaction with user.
self.imageView.isUserInteractionEnabled = true

Full code.
import UIKit

class ViewController: UIViewController {
    
    @IBOutlet var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let tapGestureRecognizer = UITapGestureRecognizer(target: self,
                                                          action: #selector(imageTappedTriple))
        tapGestureRecognizer.numberOfTapsRequired = 3
        self.imageView.isUserInteractionEnabled = true
        self.imageView.addGestureRecognizer(tapGestureRecognizer)
    }
    
    @objc func imageTappedTriple(sender: UITapGestureRecognizer) {
        print("Image was tapped three times!")
    }

}

Monday, February 27, 2017

3 ways to hide keyboard in ios

There is one of most popular issues in ios development. How to hide keyboard on touch outside or when Return button is tapped in keyboard. There are different ways how to do it.


1. override func touchesBegan


The easiest one. Just override one method of UIView - touchesBegan and invoke method endEditing of view.

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
   
}

2. Using UITapGestureRecognizer


Create gesture recognizer for tap gestures and add it to the view. On gesture handler hide the keyboard.

class ViewController: UIViewController {
    
    @IBOutlet weak var textField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let tapRecognizer = UITapGestureRecognizer(target: self, 
            action: #selector(ViewController.dismissKeyboard))
        self.view.addGestureRecognizer(tapRecognizer)
    }
    
    func dismissKeyboard() {
        self.textField.resignFirstResponder()
    }
    
}

3. Using UITextFieldDelegate

There is how to hide keyboard when Return button of Keyboard is tapped. We need to be UITextFieldDelegate, make self as delegate and implement textFieldShouldReturn method in which we will hide keyboard.

class ViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var textField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.textField.delegate = self
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.dismissKeyboard()
        return false
    }
   
}