如何在Xamarin.iOS中使用动画移动地图注释?

时间:2013-10-31 13:09:40

标签: ios mapkit xamarin key-value-observing

我有自定义注释(MKAnnotation继承者),我想在AddAnnotation之后移动它。

Coordinate属性被覆盖,但KVO(基于setCoordinate:的动画)似乎不起作用。

1 个答案:

答案 0 :(得分:3)

我通过在willChangeValue属性的setter中手动调用KVO didChangeValueCoordinate方法解决了这个问题。像这样:

    public override CLLocationCoordinate2D Coordinate 
    {
        get
        {
            var loc = UserInfo.Activity.MapLocation.Location;
            return new CLLocationCoordinate2D(loc.Latitude, loc.Longitude);
        }

        set
        {
            WillChangeValue("coordinate");

            //Save value here to some backing store
            //e.g. _coordinate = value

            DidChangeValue("coordinate");
        }
    }

像那样动画:

UIView.AnimateNotify(1.0, () => {
    myAnnotation.Coordinate = targetCoordinates;
}, finished => {

});

我也有问题,如果添加几个注释,其中一个从左上角移动。我通过将动画代码包装在后台线程中来解决它:

System.Threading.ThreadPool.QueueUserWorkItem(state => {
   InvokeOnMainThread(() => {
       UIView.AnimateNotify(1.0, () => {
           myAnnotation.Coordinate = targetCoordinates;
           secondAnnotation.Coordinate = secondCoordinates;
       }, finished => {

       });
   });
});

有点hacky,但它确实有效。它提供了调用AddAnnotations返回的机会的方法,然后更新坐标。您可能会对动画延迟做同样的事情,但它对我不起作用。