这是关于使用MKMapKit的iPhone应用程序:子类MKAnnotationView和压倒一切的setDragState
我创建了一个自定义的MKAnnotationView的可拖动注释。我想创建一个自定义动画。我设置自定义图钉图像和注释是可拖动(其中两个是这里没有显示,它的MapView发生)用下面的代码:
- (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");
}
}
一切工作正常,注释向上移动了一下,是可拖动,当我释放注释时,mapview会收到“dragstateending”。
但现在我希望动画运行在一段时间内,改变dragStateStarting以下几点:
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]; }];
}
的动画运行通缉了第二期和注释拖动。但是当我释放注释时,mapview没有通过委托来接收结尾。我也认识到,当我在开始拖动之后以“UIView animateWithDuration ...”进行动画时,随着动画开始,注释的气球开启。当我在没有动画的情况下设置新中心时,气球保持关闭状态,只有在释放注释完成拖动后才能打开气球。
我在做什么错?这是覆盖setDragState的正确方法。我真的不得不打电话给超级班吗?但是,如果没有在超类中设置dragstate,我的mapview没有意识到dragstate的变化。
我想知道MKPinAnnotationView的原始实现,但是因为它是一个内部类,我无法找到setDragState方法的描述。
Thx寻求帮助。欢呼声中,
本
它是如何完美运作的?当您将该引脚拖到屏幕外时,会导致崩溃。 – akshay1188 2012-01-13 08:24:19
这有效,但有一件事让我感到困惑:在我的MKMapViewDelegate中,我重写了mapView:annotationView:didChangeDragState。 MKAnnotationView基类产生两个转换:拖动开始时从0-> 1,当结束时从1-> 4。上述解决方案似乎遵循苹果公司对T的指导原则,但它会产生0→2和2→0的转换。我可以忍受它,但它很奇怪 - 我认为苹果公司的文档是关闭的,说实话。 – n13 2012-03-17 08:38:27
我想你需要调用'self.dragState = ...',而不是'dragState ='。否则,KVO通知将不会发送,并且地图视图不会看到拖动状态更改。 – Rick 2012-08-03 03:52:44