question

BananaSupreme avatar image
0 Votes"
BananaSupreme asked RobCaplan edited

Is there an equivalent to xamarin.android DispatchTouchEvent() in xamarin.iOS

I Am making a grid that sends its touch events down to its children(So I can have different scrollviews attached all scrolling together)

I made this custom renderer for android

 public class ScrollableGridRenderer : ViewRenderer
 {
     ScrollableGrid? _control;
     bool _elementChanged;
    
     readonly IList<Android.Views.View> _children;
    
     public ScrollableGridRenderer(Context context) : base(context)
     {
         _children = new List<Android.Views.View>();
     }
    
     protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
     {
         base.OnElementChanged(e);
    
         if (e.NewElement is ScrollableGrid element)
         {
             _control = element;
             _elementChanged = true;
         }
     }
    
     public override bool OnInterceptTouchEvent(MotionEvent ev)
     {
         if (_control == null) return false;
         if (!_elementChanged) return true;
         _children.Clear();
         for (int i = 0; i < _control.Children.Count; i++)
         {
             Android.Views.View? child = GetChildAt(i);
             if (child != null) _children.Add(child);
         }
         _elementChanged = false;
         return true;
        
     public override bool OnTouchEvent(MotionEvent? ev)
     {
         if (_control == null) return true;
         foreach (Android.Views.View child in _children)
         {
             child.DispatchTouchEvent(ev);
         }
         return true;
     }
 }

As you can see all I do is list the children of the grid and then just send forward the touch events(I haven't tried including buttons or any gesture recongnizers but I am aware of how they might screw this up and am not including them, my tests show that for static data it work's great! Other then if the scrollview orientation is set to horizontal it's not working, but that's beside the point)

My Question is how to do the same with iOS?

Thanks!

Edit :
Here is an example of code I might use the Scrollable grid in

 <controls:ScrollableGrid Grid.Row="4"
                                      x:Name="dataGrid"
                                      ColumnDefinitions="108,*"
                                  RowDefinitions="48,*"
                                      Margin="6">
       <Label Text="Not Moving"/>
       <controls:ScrollViewRTLFix Grid.Column="1"
           Orientation="Both" <!--Its horizontal but its marked as both because for some reason the touch isn't transferred here-->
           x:Name="titleScroll">
        <StackLayout Orientation="Horizontal"
                               Spacing="6">
             <Label Text="Header"/>
             <Label Text="Header"/>
             <Label Text="Header"/>
         </StackLayout>
                 </controls:ScrollViewRTLFix>
                 <controls:ScrollViewRTLFix Grid.Row="1"
                             Margin="4,0"
                             Orientation="Vertical"
                             x:Name="dayScroll">
                     <StackLayout x:Name="dayList"/> <!-- dataTable and dayList are updated dinamically in the code behind which is why they are empty here -->
                 </controls:ScrollViewRTLFix>
                 <controls:ScrollViewRTLFix Orientation="Both"
                             Grid.Row="1" Grid.Column="1"
                             x:Name="dataScroll">
                     <Grid ColumnDefinitions="108,108,108"
                           x:Name="dataTable"/> <!-- dataTable and dayList are updated dinamically in the code behind which is why they are empty here -->
                 </controls:ScrollViewRTLFix>
             </controls:ScrollableGrid>


dotnet-xamarin
· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, BananaSupreme. According to the posted code, the OnInterceptTouchEvent returns true which means the MotionEvent is intercepted and the child view cannot be clickable. The touch event in iOS is different, it doesn't provide the intercept the event. What feature do you want to achieve? Could you post more details about that. So that we can help to get another way.


0 Votes 0 ·

Hi, thanks for the reply.

After intercepting the event I am dispatching the touch event to all the children of the scrollablegrid.
What I am trying to achieve is a view that allows me to synchronize multiple ScrollViews that might be mixed in term of their orientation and some views might be static.

An example of a control I am using such a tool for is a datagrid with a frozen row and frozen column,
when scrolling over the ScrollableGrid, all the different parts of the data grid recieve the same input an thus scroll in the relative directions.

I am doing it more generally so I can reuse it for other tools as well.

0 Votes 0 ·

After intercepting the event I am dispatching the touch event to all the children of the scrollablegrid.

Does the custom grid contain the clickable view like button? When adding a tap gesture to the grid, the child views(such as Label, Image) will also trigger the gesture event.

0 Votes 0 ·
Show more comments

0 Answers