How to get Children's of the Grid using TapGestureRecognizer ?

sagarsagar ✭✭Member ✭✭
edited April 9 in Xamarin.Forms

When i tap on grid row i need to retrieve the children of grid.

Here is xaml code

<Grid Padding="15">
       <Label Text="{Binding MainMenuTitle}" FontSize="16" VerticalOptions="Center" HorizontalOptions="Start"/>
       <Image x:Name="im" Source="{Binding ArrowIconSource}" HeightRequest="15" WidthRequest="15" VerticalOptions="Cente                                          
                   HorizontalOptions="End">

       </Image>
       <Grid.GestureRecognizers>
                <TapGestureRecognizer  Tapped="Handle_Tapped" NumberOfTapsRequired="1" />
        </Grid.GestureRecognizers>
 </Grid>

In C#
async void Handle_Tapped(object sender, EventArgs e)
{
//Here i need to get the image and i need to rotate that image
}

Please help me out here

Answers

  • chetanrawatchetanrawat ✭✭✭ USMember ✭✭✭

    Hi, @sagar

    you can directly use image by Name

    async void Handle_Tapped(object sender, EventArgs e)
    {
    Task.Delay(200);
    im.RotateTo(180);
    //Here i need to get the image and i need to rotate that image
    }

  • LandLuLandLu Xamurai Member, Xamarin Team Xamurai

    You can't get the touch coordinates though tap gesture. But you could detect whether the user has clicked the scope of this image via custom renderer.
    iOS part:

    [assembly: ExportRenderer(typeof(CustomGrid), typeof(MyViewRenderer))]
    namespace Demo.iOS
    {
        public class MyViewRenderer : ViewRenderer
        { 
            public override void TouchesBegan(NSSet touches, UIEvent evt)
            {
                base.TouchesBegan(touches, evt);
    
                CGRect frame = CGRect.Empty;
                foreach (UIView subView in Subviews)
                {
                    if (subView is ImageRenderer)
                    {
                        frame = subView.Frame;
                        break;
                    }
                }
    
                UITouch touch = touches.AnyObject as UITouch;
                CGPoint location = touch.LocationInView(this);
                if (frame.Contains(location))
                {
                    // Notify the forms to move the image
                }
            }
        }
    }
    

    Android side:

    [assembly: ExportRenderer(typeof(CustomGrid), typeof(MyViewRenderer))]
    namespace Demo.Droid
    {
        public class MyViewRenderer : ViewRenderer<Grid, Android.Views.View>
        {
            public MyViewRenderer(Context context) : base(context)
            {
    
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Grid> e)
            {
                base.OnElementChanged(e);
    
                SetOnTouchListener(new MyTouchListener(this));
            }
        }
    
        public class MyTouchListener : Java.Lang.Object, Android.Views.View.IOnTouchListener
        {
            MyViewRenderer nativeView;
            public MyTouchListener(MyViewRenderer view)
            {
                nativeView = view;
            }
    
    
            public bool OnTouch(Android.Views.View v, MotionEvent e)
            {
    
                if (e.Action == MotionEventActions.Down)
                {
                    float imageX = 0;
                    float imageY = 0;
                    float imageWidth = 0;
                    float imageHeigt = 0;
    
                    for (int i = 0; i < nativeView.ChildCount; i++)
                    {
                        Android.Views.View child = nativeView.GetChildAt(i);
                        if (child.GetType() == typeof(ImageRenderer))
                        {
                            imageX = child.GetX();
                            imageY = child.GetY();
                            imageWidth = child.Width;
                            imageHeigt = child.Height;
                            break;
                        }
                    }
                    if (e.GetX() > imageX && e.GetX() < imageX + imageWidth && e.GetY() > imageY && e.GetY() < imageY + imageHeigt)
                    {
                        // Notify the forms to move the image
                    }
                }
    
                return false;
            }
        }
    }
    
Sign In or Register to comment.