-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASControlNode+Touches.swift
51 lines (44 loc) · 1.3 KB
/
ASControlNode+Touches.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// ASControlNode+Touches.swift
// pinknoise
//
// Created by Adam Tait on 4/20/18.
// Copyright © 2018 Sisterical Inc. All rights reserved.
//
import Foundation
import AsyncDisplayKit
extension ASControlNode
// keeps track of the number of times that it's touched (up inside)
// implementors should observe MutableControlNode.touches and
// perform actions based on changes
// To begin, call ASControlNode.enableTouches()
{
private static let association = ObjectAssociation<MutableProperty<Int>>()
var touches: MutableProperty<Int>
{
get {
if let v = ASControlNode.association[self] { return v }
// else create+save new
let v = MutableProperty<Int>(0)
ASControlNode.association[self] = v
return v
}
}
// public interface
// Implementors: you need to call .enableTouches() on init
func enableTouches()
{
self.isEnabled = true
self.isUserInteractionEnabled = true
self.addTarget(self,
action: #selector(self.touched),
forControlEvents: .touchUpInside)
}
// observers
@objc func touched()
{
let t = touches
let v = t.get()! + 1
t.set(v)
}
}