我正在尝试为拖动的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];
}
}

dragPin是标头中声明的MKAnnotationView。当销的dragState转到MKAnnotationViewDragStateDragging(从委托方法)时,将调用此函数。

我的目标是添加某种指示符,以指示当前所拖动的销钉的位置。

最佳答案

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

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

@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;
}

关于ios - 显示拖动的MKAnnotationPin的位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15080512/

10-09 01:50