iOS Xamarin.Forms中CustomImageRenderer监听触摸事件获取x/y坐标的方法
解答
触摸坐标获取说明
可以正常获取触摸操作的x轴和y轴坐标,以下是iOS Xamarin场景下和你现有Android实现逻辑对齐的极简示例:
using UIKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; using YourNamespace.Controls; // 替换为你自定义CustomImage所在的命名空间 [assembly: ExportRenderer(typeof(CustomImage), typeof(CustomImageRenderer))] namespace YourNamespace.iOS.Renderers { public class CustomImageRenderer : ImageRenderer { public override void TouchesBegan(NSSet touches, UIEvent evt) { base.TouchesBegan(touches, evt); var touch = touches.AnyObject as UITouch; if (touch == null) return; // 获取触摸点在当前Image控件上的相对坐标,和Android端GetX/GetY返回值逻辑一致 var touchPoint = touch.LocationInView(this); nfloat Xaxis = touchPoint.X; nfloat Yaxis = touchPoint.Y; // 此处添加你的坐标业务处理逻辑 } } }
补充说明
- 如果需要监听触摸移动、抬起事件,可对应重写
TouchesMoved、TouchesEnded方法,坐标获取逻辑完全相同 - 若需要获取相对于屏幕的绝对坐标,将
LocationInView的参数改为UIScreen.MainScreen即可
内容的提问来源于stack exchange,提问作者newxamarin
相关产品推荐
相关产品推荐

