【问题标题】:Xamarin Forms Map Tap Gesture IOSXamarin Forms Map Tap Gesture IOS
【发布时间】:2018-02-24 21:31:45
【问题描述】:

我为 Xamarin Forms Map 实现了一个自定义渲染器以实现 Tap 事件。

我是 PCL 我有这个代码:

public class ExtMap : Map
    {
        /// <summary>
        /// Event thrown when the user taps on the map
        /// </summary>
        public event EventHandler<MapTapEventArgs> Tapped;

        #region Constructors

        /// <summary>
        /// Default constructor
        /// </summary>
        public ExtMap()
        {

        }

        /// <summary>
        /// Constructor that takes a region
        /// </summary>
        /// <param name="region"></param>
        public ExtMap(MapSpan region)
            : base(region)
        {

        }

        #endregion

        public void OnTap(Position coordinate)
        {
            OnTap(new MapTapEventArgs { Position = coordinate });
        }

        protected virtual void OnTap(MapTapEventArgs e)
        {
            var handler = Tapped;

            if (handler != null)
                handler(this, e);
        }
    }

在我的 IOS 项目中,这段代码:

public class ExtMapRenderer : MapRenderer
    {
        private readonly UITapGestureRecognizer _tapRecogniser;

        public ExtMapRenderer()
        {
            _tapRecogniser = new UITapGestureRecognizer(OnTap)
            {
                NumberOfTapsRequired = 1,
                NumberOfTouchesRequired = 1
            };
        }

        private void OnTap(UITapGestureRecognizer recognizer)
        {
            var cgPoint = recognizer.LocationInView(Control);

            var location = ((MKMapView)Control).ConvertPoint(cgPoint, Control);

            ((ExtMap)Element).OnTap(new Position(location.Latitude, location.Longitude));
        }

        protected override void OnElementChanged(ElementChangedEventArgs<View> e)
        {
            if (Control != null)
                Control.RemoveGestureRecognizer(_tapRecogniser);

            base.OnElementChanged(e);

            if (Control != null)
            {
                var nativeMap = Control as MKMapView;
                nativeMap.ShowsUserLocation = true;
                Control.AddGestureRecognizer(_tapRecogniser);
            }

        }
    }

在模拟器中有时会引发事件,但我必须在地图中随机点击很多时间。 在我的 iPhone 中,从不引发该事件。

在 Android 手机和模拟器中,该事件正常运行,因此我怀疑 IOS 项目中的实施不当,但我不知道如何改进它。

【问题讨论】:

    标签: xamarin xamarin.ios xamarin.forms


    【解决方案1】:

    我有一个 iOS 地图渲染器,其中点击工作正常。

    我的 OnElementChanged 和你的有点不同:

    private MKMapView Map => Control as MKMapView;
    
    protected override void OnElementChanged(ElementChangedEventArgs<View> e)
    {
        base.OnElementChanged(e);
    
        if (e.OldElement != null && Map != null)
        {
            Control?.RemoveGestureRecognizer(_tapRecogniser);
        }
    
        if (e.NewElement != null)
        {
            Control?.AddGestureRecognizer(_tapRecogniser);
        }
    }
    

    【讨论】:

    • 这更好,但有时它不起作用....在我之前的实现中,我根本没有使用 MKMapView,可能我错过了什么?
    猜你喜欢
    • 2016-08-19
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多