我正在尝试为拖拽的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。当引脚的dragState转到MKAnnotationViewDragStateDragging (从委托方法)时,调用此函数。
我的目标是添加某种类型的指示器,指示被拖拽的引脚当前所在的位置。
发布于 2013-02-27 11:58:24
如果要向标准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显然,您需要在MKMapView委托中相应地定制viewForAnnotation。这是一个极简主义版本,但您显然会根据您的需要(标注、标题、副标题等)来调整您的版本:
- (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;
}https://stackoverflow.com/questions/15080512
复制相似问题