Showing posts with label touchesbegan. Show all posts
Showing posts with label touchesbegan. Show all posts

Friday, December 9, 2016

Part 5 - UIView's Multiple Touch Property

  • By default a UIView can only receive one touch.
  • Enabling Multiple Touch allows the UIView to receive more than one touch.
  • In code you reference the multipleTouchEnabled property

Example

In this example the tan area has Multiple Touch on while the white area does not.


Code

 
class ViewController: UIViewController {

    @IBOutlet weak var touchesLabel: UILabel!
    @IBOutlet weak var touchPointsLabel: UILabel!
    
    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        // Number of Touches
        touchesLabel.text = "\(touches.count)"
        
        // Coordinates of the Touches
        var coordinates = ""
        var touchNumber = 0
        
        for touch in touches {
            touchNumber = touchNumber + 1
            let point = touch.location(in: view)
            coordinates = coordinates + "Touch \(touchNumber): [\(point.x), \(point.y)] \n"
        }
        
        touchPointsLabel.text = coordinates
    }
}
 



(Xcode 8, Swift 3.0)

Wednesday, October 26, 2016

Dismiss Keyboard on Touch

If you want to dismiss the keyboard when you touch anywhere outside a text field, choose one of these ways:

Dismiss keyboard in a view with no scrollview/tableview

 
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    view.endEditing(true)
}
 
(Note: This will not work on a scrollview or tableview.)


Dismiss keyboard when touching inside a scrollview/tableview

  1. Add a TapGestureRecognizer to your storyboard's scene
  2. Create an action outlet in your viewcontroller with the code below
 
@IBAction func TapGestureRecognizer_Action(_ sender: UITapGestureRecognizer) {
    view.endEditing(true)
}
 


All in code

 
override func viewDidLoad() {
    super.viewDidLoad()
    
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.endEditing))
    view.addGestureRecognizer(tapGesture)
}

func endEditing() {
    view.endEditing(true)
}
 
(Xcode 8, Swift 3.0)

SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...

In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...