Solution
@IBOutlet weak var firstLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(self.firstLabelTap(sender:)))
firstLabel.isUserInteractionEnabled = true
firstLabel.addGestureRecognizer(tap)
}
@objc func firstLabelTap(sender:UITapGestureRecognizer) {
// tap works
// add your logic
}
Discussion
We can use brief taps on the screen to implement button-like interactions with any kind of content.
Tap gestures detect one or more fingers touching the screen briefly.
The fingers involved in these gestures must not move significantly from the initial touch points, and you can configure the number of times the fingers must touch the screen.
For example, you might configure tap gesture recognizers to detect single taps, double taps, or triple taps.
You can attach a gesture recognizer in one of these ways:
1. Programmatically. Call the addGestureRecognizer(_:)
method of your view.
2. In Interface Builder. Drag the appropriate object from the library and drop it onto your view.
A UITapGestureRecognizer
object provides event handling capabilities similar to those of a button—it detects a tap in its view and reports that tap to your action method.
UITapGestureRecognizer
It’s concrete subclass of UIGestureRecognizer
that looks for single or multiple taps.
Declaration
class UITapGestureRecognizer : UIGestureRecognizer
Overview
For the gesture to be recognized, the specified number of fingers must tap the view a specified number of times.
Although taps are discrete gestures, they are discrete for each state of the gesture recognizer; thus the associated action message is sent when the gesture begins and is sent for each intermediate state until (and including) the ending state of the gesture.
Code that handles tap gestures should therefore test for the state of the gesture, for example:
func handleTap(sender: UITapGestureRecognizer) {
if sender.state == .ended {
// handling code
}
}
Action methods handling this gesture may get the location of the gesture as a whole by calling the UIGestureRecognizer
method location(in:);
if there are multiple taps, this location is the first tap; if there are multiple touches, this location is the centroid of all fingers tapping the view.
Clients may get the location of particular touches in the tap by calling location(ofTouch:in:)
; if multiple taps are allowed, this location is that of the first tap.
Tap gestures are discrete, so your action method is called only when the tap gesture is recognized successfully.
You can configure a tap gesture recognizer to require any number of taps—for example, single taps or double taps—before your action method is called.
Always check the gesture recognizer’s state property before taking any actions, even for a discrete gesture recognizer.
If the code for your tap gesture recognizer is not called, check to see if the following conditions are true, and make corrections as needed:
1. The isUserInteractionEnabled
property of the view is set to true. Image views and labels set this property to false by default. (I HAVE USED IT IN THE SOLUTION ABOVE.)
2. The number of taps was equal to the number specified in the numberOfTapsRequired
property.
3. The number of fingers was equal to the number specified in the numberOfTouchesRequired
property.
addGestureRecognizer(_:)
It attaches a gesture recognizer to the view.
Declaration
func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer)
Attaching a gesture recognizer to a view defines the scope of the represented gesture, causing it to receive touches hit-tested to that view and all of its subviews.
The view establishes a strong reference to the gesture recognizer.
isUserInteractionEnabled
It’s a Boolean value that determines whether user events are ignored and removed from the event queue.
Declaration
var isUserInteractionEnabled: Bool { get set }
When set to false, touch, press, keyboard, and focus events intended for the view are ignored and removed from the event queue.
When set to true, events are delivered to the view normally. The default value of this property is true.
During an animation, user interactions are temporarily disabled for all views involved in the animation, regardless of the value in this property.
You can disable this behavior by specifying the allowUserInteraction
option when configuring the animation.
Sources
- Apple Dev Docs: uitapgesturerecognizer
- Apple Dev Docs: handling_tap_gestures
- Apple Dev Docs: addgesturerecognizer
- Apple Dev Docs: isuserinteractionenabled
app Apple error iOS mobile solution UILabel