Search code examples
swifttouchesbegantouchesended

how to use touchesBegan and touchesEnded?


I added below codes on ViewController

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("started")
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("ended")
}

but it doesn't print any messages at all.


Solution

  • UIResponder class is an abstract interface for responding to and handling events Apple API Reference

    UIWindow, UIViewController, UIView are subclass of UIResponder

    if you want to use touchesBegan and touchesEnded, override it

    touch event follow responder chain refer apple docs

    enter image description here

    some touch event occur and your ViewController's view become next responder and hittest successfully touchesBegan called when touch began and touchesEnded called touch ended