I wanted to understand how UITapGestureRecognizer
worked, so I built a simple iOS app that demonstrates how it works.
The app loads with a gray square in the middle of the screen. If you tap anywhere on that square, the label at the top of the screen will show the exact location (CGPoint
) of where you tapped.
- You can add a
UIGestureRecognizer
to anyUIView
. In this example,funView
is aUIView
that was added as anIBOutlet
in my ViewController.
// in ViewController \ viewDidLoad()
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(changeColor))
gestureRecognizer.numberOfTapsRequired = 3
funView.addGestureRecognizer(gestureRecognizer)
- Define a function that does the actions you want after the user taps that view. Also, pass that function as a parameter to the initializer in step 1.
@objc func changeColor(_ sender: UITapGestureRecognizer) {
funView.backgroundColor = UIColor.red
let tappedView = sender.view!
tappedView.layer.borderWidth = 5.0
tappedView.layer.borderColor = UIColor.purple.cgColor
}
Other notes:
- One-to-one relationship between
UIGestureRecognizer
andUIView
UITapGestureRecognizer
has a property known aslocation
which returns aCGPoint
- The
location
is relative to the top left corner the UIView. - The top left corner of the UIView is the origin or
(0.0, 0.0)
- What is the unit of measure of the CGPoint? The official answer is complicated.
A CGPoint, CGRect, or CGSize structure does not explicitly define the unit of measure for its member quantities. A point’s x- and y-coordinates or a size’s width and height are unitless quantities—whether such measurements are treated as pixels, scale-factor-independent points, texture elements (texels), or some other unit depends on the API using the measurement, and on the context in which that API is used.
- Swift 5
- Xcode 11.4
Jim Campagno's tutorial on UITapGestureRecognizer