Skip to content

Commit ff081de

Browse files
authored
fix(core): register touch animations once per view (#11641)
1 parent 87415e0 commit ff081de

1 file changed

Lines changed: 98 additions & 52 deletions

File tree

‎packages/core/ui/gestures/touch-manager.ts‎

Lines changed: 98 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,34 @@ export type VisionHoverOptions = {
2626
shapeCornerRadius?: number;
2727
};
2828

29+
/**
30+
* What TouchManager.addAnimations has wired for a view. Kept on the view, so it lives exactly as long as the view does.
31+
*/
32+
const touchState = Symbol('touchState');
33+
type TouchState = {
34+
/** The down animation is hooked to the view's touchDown event. */
35+
down?: boolean;
36+
/** The up animation is hooked to the view's touchUp event. */
37+
up?: boolean;
38+
/** The gesture that raises touchDown/touchUp is attached: touch on Android, long press on iOS views that are not a UIControl. */
39+
gesture?: boolean;
40+
/** Target of the UIControl events. Held here because a UIControl does not retain its targets. */
41+
handler?: any /* TouchControlHandler */;
42+
/** The handler is registered with the current native view for down. */
43+
nativeDown?: boolean;
44+
/** The handler is registered with the current native view for up. */
45+
nativeUp?: boolean;
46+
disposeHook?: boolean;
47+
};
48+
49+
function getTouchState(view: View): TouchState {
50+
const holder = view as unknown as { [touchState]?: TouchState };
51+
if (!holder[touchState]) {
52+
holder[touchState] = {};
53+
}
54+
return holder[touchState];
55+
}
56+
2957
/**
3058
* Manage interactivity in your apps easily with TouchManager.
3159
* Store reusable down/up animation settings for touches as well as optionally enable automatic tap (down/up) animations for your app.
@@ -66,8 +94,7 @@ export class TouchManager {
6694
// }
6795
static animations: TouchAnimationOptions;
6896
/**
69-
* Native Touch handlers (iOS only) registered with the view through the TouchManager.
70-
* The TouchManager uses this internally but makes public for other versatility if needed.
97+
* @deprecated No longer populated. The native touch handler (iOS only) is kept with its view and released with it.
7198
*/
7299
static touchHandlers: Array<{ view: View; handler: any /* UIGestureRecognizer */ }>;
73100
/**
@@ -84,59 +111,74 @@ export class TouchManager {
84111
const handleDown = (view?.touchAnimation && (<TouchAnimationOptions>view?.touchAnimation).down) || (TouchManager.animations && TouchManager.animations.down);
85112
const handleUp = (view?.touchAnimation && (<TouchAnimationOptions>view?.touchAnimation).up) || (TouchManager.animations && TouchManager.animations.up);
86113

114+
// View.onLoaded calls this on every load, not only the first one, and a view can gain a
115+
// direction between two loads. Down and up are tracked separately so each is wired once:
116+
// the listeners per view, the control target per native view.
117+
const state = getTouchState(view);
118+
const wireDown = !!handleDown && !state.down;
119+
const wireUp = !!handleUp && !state.up;
120+
if (wireDown) {
121+
state.down = true;
122+
}
123+
if (wireUp) {
124+
state.up = true;
125+
}
126+
87127
if (__APPLE__) {
88128
if (view?.ios?.addTargetActionForControlEvents) {
89129
// can use UIControlEvents
90-
if (!TouchManager.touchHandlers) {
91-
TouchManager.touchHandlers = [];
130+
if (!state.handler) {
131+
state.handler = TouchControlHandler.initWithOwner(new WeakRef(view));
132+
}
133+
if (handleDown && !state.nativeDown) {
134+
state.nativeDown = true;
135+
(<UIControl>view.ios).addTargetActionForControlEvents(state.handler, GestureEvents.touchDown, UIControlEvents.TouchDown | UIControlEvents.TouchDragEnter);
136+
}
137+
if (handleUp && !state.nativeUp) {
138+
state.nativeUp = true;
139+
(<UIControl>view.ios).addTargetActionForControlEvents(state.handler, GestureEvents.touchUp, UIControlEvents.TouchDragExit | UIControlEvents.TouchCancel | UIControlEvents.TouchUpInside | UIControlEvents.TouchUpOutside);
92140
}
93-
TouchManager.touchHandlers.push({
94-
view,
95-
handler: TouchControlHandler.initWithOwner(new WeakRef(view)),
96-
});
97141

98-
if (handleDown) {
99-
(<UIControl>view.ios).addTargetActionForControlEvents(TouchManager.touchHandlers[TouchManager.touchHandlers.length - 1].handler, GestureEvents.touchDown, UIControlEvents.TouchDown | UIControlEvents.TouchDragEnter);
142+
if (wireDown) {
100143
view.on(GestureEvents.touchDown, (args) => {
101144
TouchManager.startAnimationForType(view, TouchAnimationTypes.down);
102145
});
103146
}
104-
if (handleUp) {
105-
(<UIControl>view.ios).addTargetActionForControlEvents(TouchManager.touchHandlers[TouchManager.touchHandlers.length - 1].handler, GestureEvents.touchUp, UIControlEvents.TouchDragExit | UIControlEvents.TouchCancel | UIControlEvents.TouchUpInside | UIControlEvents.TouchUpOutside);
147+
if (wireUp) {
106148
view.on(GestureEvents.touchUp, (args) => {
107149
TouchManager.startAnimationForType(view, TouchAnimationTypes.up);
108150
});
109151
}
110-
} else {
111-
if (handleDown || handleUp) {
112-
view.on(GestureEvents.gestureAttached, (args: GestureEventData) => {
113-
if (args.type === GestureTypes.longPress) {
114-
(<UILongPressGestureRecognizer>args.ios).minimumPressDuration = (<View>args.object)?.touchDelay || 0;
115-
}
116-
});
117-
view.on(GestureTypes[GestureTypes.longPress], (args: GestureEventDataWithState) => {
118-
switch (args.state) {
119-
case GestureStateTypes.began:
120-
if (handleDown) {
121-
TouchManager.startAnimationForType(<View>args.view, TouchAnimationTypes.down);
122-
}
123-
break;
124-
case GestureStateTypes.cancelled:
125-
case GestureStateTypes.ended:
126-
if (handleUp) {
127-
TouchManager.startAnimationForType(<View>args.view, TouchAnimationTypes.up);
128-
}
129-
break;
130-
}
131-
});
132-
}
152+
} else if ((state.down || state.up) && !state.gesture) {
153+
state.gesture = true;
154+
view.on(GestureEvents.gestureAttached, (args: GestureEventData) => {
155+
if (args.type === GestureTypes.longPress) {
156+
(<UILongPressGestureRecognizer>args.ios).minimumPressDuration = (<View>args.object)?.touchDelay || 0;
157+
}
158+
});
159+
view.on(GestureTypes[GestureTypes.longPress], (args: GestureEventDataWithState) => {
160+
switch (args.state) {
161+
case GestureStateTypes.began:
162+
if (state.down) {
163+
TouchManager.startAnimationForType(<View>args.view, TouchAnimationTypes.down);
164+
}
165+
break;
166+
case GestureStateTypes.cancelled:
167+
case GestureStateTypes.ended:
168+
if (state.up) {
169+
TouchManager.startAnimationForType(<View>args.view, TouchAnimationTypes.up);
170+
}
171+
break;
172+
}
173+
});
133174
}
134175
} else {
135-
if (handleDown || handleUp) {
176+
if ((state.down || state.up) && !state.gesture) {
177+
state.gesture = true;
136178
view.on(GestureTypes[GestureTypes.touch], (args: TouchGestureEventData) => {
137179
switch (args.action) {
138180
case 'down':
139-
if (handleDown) {
181+
if (state.down) {
140182
view.notify({
141183
eventName: GestureEvents.touchDown,
142184
object: view,
@@ -146,7 +188,7 @@ export class TouchManager {
146188
break;
147189
case 'up':
148190
case 'cancel':
149-
if (handleUp) {
191+
if (state.up) {
150192
view.notify({
151193
eventName: GestureEvents.touchUp,
152194
object: view,
@@ -156,24 +198,28 @@ export class TouchManager {
156198
break;
157199
}
158200
});
159-
if (handleDown) {
160-
view.on(GestureEvents.touchDown, (args) => {
161-
TouchManager.startAnimationForType(view, TouchAnimationTypes.down);
162-
});
163-
}
164-
if (handleUp) {
165-
view.on(GestureEvents.touchUp, (args) => {
166-
TouchManager.startAnimationForType(view, TouchAnimationTypes.up);
167-
});
168-
}
201+
}
202+
if (wireDown) {
203+
view.on(GestureEvents.touchDown, (args) => {
204+
TouchManager.startAnimationForType(view, TouchAnimationTypes.down);
205+
});
206+
}
207+
if (wireUp) {
208+
view.on(GestureEvents.touchUp, (args) => {
209+
TouchManager.startAnimationForType(view, TouchAnimationTypes.up);
210+
});
169211
}
170212
}
171213

214+
if (state.disposeHook) {
215+
return;
216+
}
217+
state.disposeHook = true;
172218
view.on('disposeNativeView', (args) => {
173-
const index = TouchManager.touchHandlers?.findIndex((handler) => handler.view === args.object);
174-
if (index > -1) {
175-
TouchManager.touchHandlers.splice(index, 1);
176-
}
219+
// The handler and its registrations belong to the native view that is going away.
220+
state.handler = undefined;
221+
state.nativeDown = false;
222+
state.nativeUp = false;
177223
TouchManager.touchAnimationDefinitions = TouchManager.touchAnimationDefinitions?.filter((d) => d.view !== args.object);
178224
});
179225
}

0 commit comments

Comments
 (0)