how to use touchesBegan and touchesEnded?

3.9k Views Asked by At

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.

1

There are 1 best solutions below

1
Cruz On BEST ANSWER

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