Class
UITapGestureRecognizer
is a subclass of
UIGestureRecognizer
.
AppDelegate
:
unchanged.ViewController
:
unchanged.View
:
added methods
init(coder:)
,
singleAction(_:)
,
doubleAction(_:)
,
and
wearOff()
.
Note that the last parameter of
dispatch_after
is a
dispatch_block_t
,
not a
selector.
Class
View
is a subclass of
UILabel
.
The single tap recognizer must not be allowed to recognize a single tap
until the double tap recognizer has told it that the tap is not the first
half of a double tap.
See
requireGestureRecognizerToFail:
.
NSEC_PER_SEC
= 1,000,000,000
= 109
is the number of nanoseconds per second.
I wanted to say
3.5 * NSEC_PER_SEC
instead of
3500000000
,
but Swift wouldn’t let me multiply them because
3.5
is a
Double
and
NSEC_PER_SEC
is a
UInt64
.
I could have converted
NSEC_PER_SEC
to
Double
,
multiplied it by
3.5
,
and converted the product back to the
Int64
required by
dispatch_time
,
let delay: Int64 = Int64(3.5 * Double(NSEC_PER_SEC));but it was simpler to write
3500000000
.
Note that the
3500000000
was parsed correctly
even though it was bigger than the maximum 32-bit integer
231 − 1 = 2,147,483,647.
See
Literals.
3500000000
to
3_500_000_000
to make it easier to read.
See
Integer
Literals.
singleAction(_:)
and
doubleAction(_:)
by the following.
It does the work of either one.
func action(recognizer: UITapGestureRecognizer) -> Void { text = String(recognizer.numberOfTapsRequired); //Convert Int to String. let t: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, delay); dispatch_after(t, dispatch_get_main_queue(), wearOff); }
dispatch_after(t, dispatch_get_main_queue(), wearOff);to use a closure.
dispatch_after(t, dispatch_get_main_queue(), {self.text = "0";});
wearOff()
that the previous gesture scheduled.