显示拖动的MKAnnotationPin的位置

时间:2013-02-26 02:51:46

标签: ios animation mapkit mkannotation

我正在尝试为拖动的MKAnnotationPin添加某种指示器..

到目前为止,我提出了以下非工作解决方案:

- (void) showDragLoc{


UIView *cross = [[UIView alloc] initWithFrame:CGRectMake(dragPin.center.x, dragPin.center.y, 10, 10)];
[self.mapView addSubview:cross];
while(dragPin.dragState  == MKAnnotationViewDragStateDragging){

    [UIView beginAnimations:nil context:NULL]; // animate the following:
    cross.frame = CGRectMake(dragPin.center.x, dragPin.center.y, 10, 10); // move to new location
    [UIView setAnimationDuration:0.3];
    [UIView commitAnimations];
}
}

ere dragPin是在标题中声明的MKAnnotationView。当pin的dragState转到MKAnnotationViewDragStateDragging(来自委托方法)时,将调用此函数。

我的目标是添加某种拖动销当前位置的指示器。

1 个答案:

答案 0 :(得分:1)

如果要向标准MKPinAnnotationView添加十字准线,则应将其子类化,然后在setDragState:animated:的实现中添加十字准线。

因此,要子类化,请创建一个新类,例如PinWithCrosshairAnnotationView。 .h公共接口不需要太多:

@interface PinWithCrosshairAnnotationView : MKPinAnnotationView

@end

.m实现只是实现了一个setDragState:animated:,它添加了你的十字准线,然后调用super实现来获取引脚的拉取和删除功能。如果animated标志已打开,我也会动画,但您不必这样做。您的frame坐标无疑与我的坐标不同,但我从上面的代码示例中收集到您已经为您的十字准线图像找到正确的值:

#import "PinWithCrosshairAnnotationView.h"

@interface PinWithCrosshairAnnotationView ()
@property (nonatomic, weak) UIImageView *crosshairImageView;
@end

@implementation PinWithCrosshairAnnotationView

- (void)setDragState:(MKAnnotationViewDragState)newDragState animated:(BOOL)animated
{
    if (newDragState == MKAnnotationViewDragStateStarting)
    {
        // create the crosshair imageview and add it as a subview

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(-1.5, 30, 17.5, 17.5)];
        imageView.image = [UIImage imageNamed:@"Crosshairs.png"];
        [self addSubview:imageView];

        // if the animated flag is on, we'll fade it to visible state

        if (animated)
        {
            imageView.alpha = 0.0;
            [UIView animateWithDuration:0.2
                             animations:^{
                                 imageView.alpha = 1.0;
                             }];
        }

        // save a reference to that imageview in a class property

        self.crosshairImageView = imageView;
    }
    else if (newDragState == MKAnnotationViewDragStateEnding || newDragState == MKAnnotationViewDragStateCanceling)
    {
        if (animated)
        {
            // if we're animating, let's quickly fade it to invisible
            // and in the completion block, we'll remove it

            [UIView animateWithDuration:0.2
                             animations:^{
                                 self.crosshairImageView.alpha = 0.0;
                             }
                             completion:^(BOOL finished) {
                                 [self.crosshairImageView removeFromSuperview];
                                 self.crosshairImageView = nil;
                             }];
        }
        else
        {
            // if we're not animating, just remove it

            [self.crosshairImageView removeFromSuperview];
            self.crosshairImageView = nil;
        }
    }

    // remember to call super so we get all the other wonderful superclass behavior

    [super setDragState:newDragState animated:animated];
}
@end

显然,您可以相应地自定义viewForAnnotation代表中的MKMapView。这是一个极简主义版本,但你显然会根据你的需要(标注,标题,副标题等)调整你的版本:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    MKAnnotationView *view = [[PinWithCrosshairAnnotationView alloc] initWithAnnotation:annotation
                                                                        reuseIdentifier:@"pinwithcrosshairannotation"];
    view.draggable = YES;
    view.canShowCallout = NO;

    return view;
}