You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 12:18:03