简体   繁体   English

捕获用户触摸的x和y,iOS Xamarin Forms

[英]Capture x and y of user touch, iOS Xamarin Forms

I am using Xamarin Forms (Xamarin Studio Community) to write an iOS app. 我正在使用Xamarin Forms(Xamarin Studio Community)编写iOS应用程序。 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. 它们要么是针对Android,要么是Swift,要么是XCode,要么是太多而无法实际使用。

The closest I've seen are answers that say "There is currently no way to do that in Xamarin Forms." 我见过的最接近的答案是“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. 我只需要从用户触摸和释放的位置捕获x,y。 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. 您不需要调用TouchesMoved()方法。 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. 它在Objective C运行时调用它时自动调用(它是要调用的方法的覆盖) - 它基本上是本机事件的处理程序。 You can already get the offsets, and you can raise your own events inside that method to change your button. 您已经可以获得偏移量,并且可以在该方法中引发自己的事件来更改按钮。 It is already done. 它已经完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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