简体   繁体   中英

Capture x and y of user touch, iOS Xamarin Forms

I am using Xamarin Forms (Xamarin Studio Community) to write an iOS app. I just need to capture the coordinates of user touch and release on a button.

I have searched all over Google, and I only found responses that simply did not work. They were either for Android, or Swift, or XCode, or WAY too much to make practical use of.

The closest I've seen are answers that say "There is currently no way to do that in Xamarin Forms." Which seems absurd to me because it seems like one of the most basic, fundamental things a programmer would need.

All I need is to capture the x,y from where the user touches and releases. Here are my handlers all ready to go.

    thisHereButton.TouchDown += (object sender, System.EventArgs e) =>
    {
        //pressed
        touchX = [x];
        touchY=[y];
    };
     thisHereButton.TouchUpInside += (object sender, System.EventArgs e) =>
    {
        //released
        releaseX = [x];
        releaseY=[y];
    }; 

That's it. If someone could provide a simple, practical answer, that would be great!

EDIT: With a bounty goes additional research, in hopes that someone can help.

I followed the advice in the comments and after some analysis, have determined that this is the chunk of code that is most relevant.

private bool touchStartedInside;
        public override void TouchesMoved(NSSet touches, UIEvent evt)
        {
            base.TouchesMoved(touches, evt);
            // get the touch
            UITouch touch = touches.AnyObject as UITouch;
            if (touch != null)
            {
                //==== IMAGE TOUCH
                if (backdrop.Frame.Contains(touch.LocationInView(View)))
                {
                    //TouchStatus.Text = "Touches Moved";
                }

                //==== IMAGE DRAG
                // check to see if the touch started in the drag me image
                if (touchStartedInside)
                {
                    // move the shape
                    float offsetX = (float)(touch.PreviousLocationInView(View).X - touch.LocationInView(View).X);
                    float offsetY = (float)(touch.PreviousLocationInView(View).Y - touch.LocationInView(View).Y);
                    txt_sizeValText.Text = "" + offsetY;
                    //DragImage.Frame = new RectangleF(new PointF(DragImage.Frame.X - offsetX, DragImage.Frame.Y - offsetY), DragImage.Frame.Size);
                }
         }
        }  

That is taken directly from their example. Right there in the code, it gives me what I'm looking for, as well as the bonus of the "offset" which is what I really need in the end. So I've got this void in my code and the code still compiles, so there are no errors.

The only thing I cannot figure out is how to make use of this void. How do you call it? If I have to use a different handler than the ones in my original post, that's fine. Whatever you can tell me about how to make this work...

You don't need to call the TouchesMoved() method. It is automatically called when the Objective C runtime calls it ( it is an override of the method to be called) - It is basically the handler of the native event. You can already get the offsets, and you can raise your own events inside that method to change your button. It is already done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM