子类化 MKAnnotationView 并覆盖 setDragState

Subclassing MKAnnotationView and overriding setDragState(子类化 MKAnnotationView 并覆盖 setDragState)

本文介绍了子类化 MKAnnotationView 并覆盖 setDragState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于使用 MKMapKit 的 iPhone 应用程序:

This is about an iPhone App using MKMapKit:

我为可拖动的 Annotation 创建了一个自定义 MKAnnotationView.我想创建一个自定义动画.我使用以下代码设置了一个自定义图钉图像,并且注释是可拖动的(此处均未显示,它发生在地图视图中):

I created a custom MKAnnotationView for a draggable Annotation. I want to create a custom animation. I set a custom pin image and the annotation is draggable (which both is not shown here, it happens in the mapview) with the following code:

- (void) movePinUpFinished {

     [super setDragState:MKAnnotationViewDragStateDragging];
     [self setDragState:MKAnnotationViewDragStateDragging];
}

- (void) setDragState:(MKAnnotationViewDragState) myState {
     if (myState == MKAnnotationViewDragStateStarting) {
          NSLog(@"starting");
          CGPoint endPoint = CGPointMake(self.center.x,self.center.y-20);
          self.center = endPoint;
          [self movePinUpFinished];
     }
     if (myState == MKAnnotationViewDragStateEnding) {
          NSLog(@"ending");
          [super setDragState:MKAnnotationViewDragStateEnding];
          [self setDragState:MKAnnotationViewDragStateNone];
          [super setDragState:MKAnnotationViewDragStateNone];
     }
     if (myState == MKAnnotationViewDragStateDragging) {
          NSLog(@"dragging");
     }
     if (myState == MKAnnotationViewDragStateCanceling) {
          NSLog(@"cancel");
     }
     if (myState == MKAnnotationViewDragStateNone) {
          NSLog(@"none");
     }
}

一切正常,注释向上移动一点,可拖动,当我释放注释时,地图视图收到dragstateending".

Everything works fine, the annotation is moved up a bit, is draggable and when i release the annotation, the mapview receives the "dragstateending".

但现在我希望动画在一段时间内运行并将 dragStateStarting 更改为以下内容:

But now I want the animation to run over a time period and change the dragStateStarting to the following:

if (myState == MKAnnotationViewDragStateStarting) {
          NSLog(@"starting");
          CGPoint endPoint = CGPointMake(self.center.x,self.center.y-20);
          [UIView animateWithDuration:1.0
           animations:^{ self.center = endPoint; }
           completion:^(BOOL finished){ [self movePinUpFinished]; }];
     }

动画在一秒钟内按需要运行,并且注释是可拖动的.但是当我发布注释时,地图视图没有通过委托接收到结尾.我还认识到,当我使用UIView animateWithDuration ..."制作动画时,在开始拖动后,随着动画的开始,注释的气球就会打开.当我在没有动画的情况下设置新中心时,气球保持关闭状态,只有在通过释放注释完成拖动后才会打开.

The animations runs as wanted over the period of a second and the annotation is draggable. But when I release the annotation, the mapview is not receiving the ending through the delegat. What I also recognized was that when I am doing the animation with "UIView animateWithDuration..." is that immedently after beginning the dragging, as the animation starts, the ballon of the annotation opens. When i am setting the new center without the animation, the balloon keeps closed and is only opened after finishing the dragging by releasing the annotation.

我做错了什么?这是覆盖 setDragState 的正确方法吗?我真的必须打电话给超级班吗?但是没有在超类中设置dragstate,我的mapview没有实现dragstate的变化.

What am I doing wrong? Is this the right way to override setDragState. Do I really have to call the super class? But without setting the dragstate in the superclass my mapview didnt realized the changes of the dragstate.

我想知道 MKPinAnnotationView 的原始实现,但是因为它是一个内部类,所以我找不到 setDragState 方法的描述.

I wonder about the original implementation of MKPinAnnotationView, but because it is an internal Class I couldn't find a description of the setDragState method.

谢谢帮助.干杯,

推荐答案

我让 pin 拖动工作,但试图弄清楚为什么当你不覆盖 setDragState 时会发生 pin 动画 - 在我的实现中不再工作.你的问题包含了我的答案.. 谢谢!

I had the pin drag working but was trying to figure out why the pin annimations that occur when you don't override setDragState - no longer work in my implementation. Your question contained my answer .. Thanks!

您的代码的部分问题是,一旦您覆盖了 setDragState 函数,根据 xcode 文档,您需要负责根据进入的新状态更新 dragState 变量.我也会有点担心您的代码调用自身(setDragState 调用 [self setDragState]).

Part of the problem with your code is that once you override the setDragState function, per the xcode documentation, you are responsible for updating the dragState variable based on the new state coming in. I would also be a little concerned about your code calling itself (setDragState calling [self setDragState]).

这是我最终(在您的帮助下)完成的所有提升、拖放和下降的代码,正如我所期望的那样.希望这对你也有帮助!

Here is the code I ended up (with your help) that does all of the lifts, drags and drops as I expect them to occur. Hope this helps you too!

- (void)setDragState:(MKAnnotationViewDragState)newDragState animated:(BOOL)animated
{
    if (newDragState == MKAnnotationViewDragStateStarting)
    {
        // lift the pin and set the state to dragging

        CGPoint endPoint = CGPointMake(self.center.x,self.center.y-20);
        [UIView animateWithDuration:0.2
                         animations:^{ self.center = endPoint; }
                         completion:^(BOOL finished)
                             { self.dragState = MKAnnotationViewDragStateDragging; }];
    }
    else if (newDragState == MKAnnotationViewDragStateEnding)
    {
        // save the new location, drop the pin, and set state to none

        /* my app specific code to save the new position
        objectObservations[ACTIVE].latitude = pinAnnotation.coordinate.latitude;
        objectObservations[ACTIVE].longitude = pinAnnotation.coordinate.longitude;
        posChanged = TRUE;
        */

        CGPoint endPoint = CGPointMake(self.center.x,self.center.y+20);
        [UIView animateWithDuration:0.2
                         animations:^{ self.center = endPoint; }
                         completion:^(BOOL finished)
                             { self.dragState = MKAnnotationViewDragStateNone; }];
    }
    else if (newDragState == MKAnnotationViewDragStateCanceling)
    {
        // drop the pin and set the state to none

        CGPoint endPoint = CGPointMake(self.center.x,self.center.y+20);
        [UIView animateWithDuration:0.2
                         animations:^{ self.center = endPoint; }
                         completion:^(BOOL finished)
                             { self.dragState = MKAnnotationViewDragStateNone; }];
    }
}

这篇关于子类化 MKAnnotationView 并覆盖 setDragState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:子类化 MKAnnotationView 并覆盖 setDragState

基础教程推荐