我正在使用 Xamarin Forms(Xamarin Studio 社区)编写一个 iOS 应用程序。我只需要捕获用户触摸和释放按钮的坐标。
我在整个 Google 上进行了搜索,但只找到了根本不起作用的响应。它们要么适用于 Android、Swift、XCode,要么实在太实用了。
我看到的最接近的答案是“目前无法在 Xamarin Forms 中做到这一点。”这对我来说似乎很荒谬,因为它似乎是程序员需要的最基本、最基本的东西之一。
我只需要从用户触摸和释放的位置捕获 x,y。我的处理人员已经准备就绪。
thisHereButton.TouchDown += (object sender, System.EventArgs e) =>
{
//pressed
touchX = [x];
touchY=[y];
};
thisHereButton.TouchUpInside += (object sender, System.EventArgs e) =>
{
//released
releaseX = [x];
releaseY=[y];
};
就是这样。如果有人能提供一个简单实用的答案,那就太好了!
编辑:有了赏金就可以进行额外的研究,希望有人能提供帮助。
我听从了评论中的建议并经过一些分析,确定这是最相关的代码块。
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);
}
}
}
这直接取自他们的例子。就在代码中,它给了我我正在寻找的东西,以及我最终真正需要的“偏移量”的好处。 所以我的代码中有这个空值,代码仍然可以编译,所以没有错误。
我唯一想不通的是如何利用这个空白。你怎么调用它? 如果我必须使用与原始帖子中不同的处理程序,那很好。无论你能告诉我如何使这项工作...
最佳答案
您不需要调用 TouchesMoved() 方法。当 Objective C 运行时调用它时它会自动调用(它是要调用的方法的重写)——它基本上是 native 事件的处理程序。您已经可以获得偏移量,并且可以在该方法中引发自己的事件来更改按钮。已经完成了。
关于c# - 捕获用户触摸的 x 和 y,iOS Xamarin Forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43381512/