@@ -8,24 +8,18 @@ namespace OxyPlot.Maui.Skia.ios.Effects;
8
8
9
9
public class PlatformTouchEffect : PlatformEffect
10
10
{
11
- UIView view ;
12
- TouchRecognizer touchRecognizer ;
11
+ private UIView view ;
12
+ private TouchRecognizer touchRecognizer ;
13
13
14
14
protected override void OnAttached ( )
15
15
{
16
- // Get the iOS UIView corresponding to the Element that the effect is attached to
17
16
view = Control ?? Container ;
18
17
19
- // Uncomment this line if the UIView does not have touch enabled by default
20
- //view.UserInteractionEnabled = true;
21
-
22
- // Get access to the TouchEffect class in the .NET Standard library
23
18
var touchEffect = Element . Effects . OfType < MyTouchEffect > ( ) . FirstOrDefault ( ) ;
24
19
25
20
if ( touchEffect != null && view != null )
26
21
{
27
- // Create a TouchRecognizer for this UIView
28
- touchRecognizer = new TouchRecognizer ( Element , view , touchEffect ) ;
22
+ touchRecognizer = new TouchRecognizer ( Element , touchEffect ) ;
29
23
view . AddGestureRecognizer ( touchRecognizer ) ;
30
24
}
31
25
}
@@ -34,140 +28,71 @@ protected override void OnDetached()
34
28
{
35
29
if ( touchRecognizer != null )
36
30
{
37
- // Clean up the TouchRecognizer object
38
31
touchRecognizer . Detach ( ) ;
39
-
40
- // Remove the TouchRecognizer from the UIView
41
32
view . RemoveGestureRecognizer ( touchRecognizer ) ;
42
33
}
43
34
}
44
35
}
45
36
46
- class TouchRecognizer : UIGestureRecognizer
37
+ internal class TouchRecognizer : UIGestureRecognizer
47
38
{
48
- Microsoft . Maui . Controls . Element element ; // Forms element for firing events
49
- UIView view ; // iOS UIView
50
- MyTouchEffect touchPlatformEffect ;
51
-
52
- static Dictionary < UIView , TouchRecognizer > viewDictionary = new ( ) ;
39
+ private readonly Microsoft . Maui . Controls . Element element ;
40
+ private readonly MyTouchEffect touchEffect ;
41
+ private uint activeTouchesCount = 0 ;
53
42
54
- static Dictionary < long , TouchRecognizer > idToTouchDictionary = new ( ) ;
55
-
56
- public TouchRecognizer ( Microsoft . Maui . Controls . Element element , UIView view , MyTouchEffect touchPlatformEffect )
43
+ public TouchRecognizer ( Microsoft . Maui . Controls . Element element , MyTouchEffect touchEffect )
57
44
{
58
45
this . element = element ;
59
- this . view = view ;
60
- this . touchPlatformEffect = touchPlatformEffect ;
46
+ this . touchEffect = touchEffect ;
61
47
62
- viewDictionary . Add ( view , this ) ;
48
+ ShouldRecognizeSimultaneously = new UIGesturesProbe ( ( _ , _ ) => true ) ;
63
49
}
64
50
65
51
public void Detach ( )
66
52
{
67
- viewDictionary . Remove ( view ) ;
53
+ ShouldRecognizeSimultaneously = null ;
68
54
}
69
55
70
- // touches = touches of interest; evt = all touches of type UITouch
71
56
public override void TouchesBegan ( NSSet touches , UIEvent evt )
72
57
{
73
58
base . TouchesBegan ( touches , evt ) ;
74
-
75
- foreach ( UITouch touch in touches . Cast < UITouch > ( ) )
76
- {
77
- long id = ( ( IntPtr ) touch . Handle ) . ToInt64 ( ) ;
78
- FireEvent ( this , id , TouchActionType . Pressed , touch , true ) ;
79
-
80
- if ( ! idToTouchDictionary . ContainsKey ( id ) )
81
- {
82
- idToTouchDictionary . Add ( id , this ) ;
83
- }
84
- }
59
+ activeTouchesCount += touches . Count . ToUInt32 ( ) ;
60
+ FireEvent ( touches , TouchActionType . Pressed , true ) ;
85
61
}
86
62
87
63
public override void TouchesMoved ( NSSet touches , UIEvent evt )
88
64
{
89
65
base . TouchesMoved ( touches , evt ) ;
90
66
91
- foreach ( UITouch touch in touches . Cast < UITouch > ( ) )
67
+ if ( activeTouchesCount == touches . Count . ToUInt32 ( ) )
92
68
{
93
- long id = ( ( IntPtr ) touch . Handle ) . ToInt64 ( ) ;
94
- CheckForBoundaryHop ( touch ) ;
95
- if ( idToTouchDictionary [ id ] != null )
96
- {
97
- FireEvent ( idToTouchDictionary [ id ] , id , TouchActionType . Moved , touch , true ) ;
98
- }
69
+ FireEvent ( touches , TouchActionType . Moved , true ) ;
99
70
}
100
71
}
101
72
102
73
public override void TouchesEnded ( NSSet touches , UIEvent evt )
103
74
{
104
75
base . TouchesEnded ( touches , evt ) ;
105
-
106
- foreach ( UITouch touch in touches . Cast < UITouch > ( ) )
107
- {
108
- long id = ( ( IntPtr ) touch . Handle ) . ToInt64 ( ) ;
109
- CheckForBoundaryHop ( touch ) ;
110
- if ( idToTouchDictionary [ id ] != null )
111
- {
112
- FireEvent ( idToTouchDictionary [ id ] , id , TouchActionType . Released , touch , false ) ;
113
- }
114
-
115
- idToTouchDictionary . Remove ( id ) ;
116
- }
76
+ activeTouchesCount -= touches . Count . ToUInt32 ( ) ;
77
+ FireEvent ( touches , TouchActionType . Released , false ) ;
117
78
}
118
79
119
80
public override void TouchesCancelled ( NSSet touches , UIEvent evt )
120
81
{
121
82
base . TouchesCancelled ( touches , evt ) ;
122
-
123
- foreach ( UITouch touch in touches . Cast < UITouch > ( ) )
124
- {
125
- long id = ( ( IntPtr ) touch . Handle ) . ToInt64 ( ) ;
126
- idToTouchDictionary . Remove ( id ) ;
127
- }
128
83
}
129
84
130
- void CheckForBoundaryHop ( UITouch touch )
85
+ private void FireEvent ( NSSet touches , TouchActionType actionType , bool isInContact )
131
86
{
132
- long id = ( ( IntPtr ) touch . Handle ) . ToInt64 ( ) ;
133
-
134
- // TODO: Might require converting to a List for multiple hits
135
- TouchRecognizer recognizerHit = null ;
87
+ UITouch [ ] uiTouches = touches . Cast < UITouch > ( ) . ToArray ( ) ;
88
+ long id = ( ( IntPtr ) uiTouches . First ( ) . Handle ) . ToInt64 ( ) ;
89
+ Point [ ] points = new Point [ uiTouches . Length ] ;
136
90
137
- foreach ( UIView view in viewDictionary . Keys )
138
- {
139
- CGPoint location = touch . LocationInView ( view ) ;
140
-
141
- if ( new CGRect ( new CGPoint ( ) , view . Frame . Size ) . Contains ( location ) )
142
- {
143
- recognizerHit = viewDictionary [ view ] ;
144
- }
145
- }
146
- if ( recognizerHit != idToTouchDictionary [ id ] )
91
+ for ( int i = 0 ; i < uiTouches . Length ; i ++ )
147
92
{
148
- if ( idToTouchDictionary [ id ] != null )
149
- {
150
- FireEvent ( idToTouchDictionary [ id ] , id , TouchActionType . Pressed , touch , true ) ;
151
- }
152
- if ( recognizerHit != null )
153
- {
154
- FireEvent ( recognizerHit , id , TouchActionType . Released , touch , true ) ;
155
- }
156
- idToTouchDictionary [ id ] = recognizerHit ;
93
+ CGPoint cgPoint = uiTouches [ i ] . LocationInView ( View ) ;
94
+ points [ i ] = new ( cgPoint . X , cgPoint . Y ) ;
157
95
}
96
+ touchEffect . OnTouchAction ( element , new ( id , actionType , points , isInContact ) ) ;
158
97
}
159
-
160
- void FireEvent ( TouchRecognizer recognizer , long id , TouchActionType actionType , UITouch touch , bool isInContact )
161
- {
162
- // Convert touch location to Maui Point value
163
- CGPoint cgPoint = touch . LocationInView ( recognizer . View ) ;
164
- Point xfPoint = new Point ( cgPoint . X , cgPoint . Y ) ;
165
-
166
- // Get the method to call for firing events
167
- var onTouchAction = recognizer . touchPlatformEffect . OnTouchAction ;
168
-
169
- // Call that method
170
- onTouchAction ( recognizer . element ,
171
- new TouchActionEventArgs ( id , actionType , new [ ] { xfPoint } , isInContact ) ) ;
172
- }
173
- }
98
+ }
0 commit comments