【发布时间】:2012-01-11 15:27:50
【问题描述】:
我在使用 UILongPressGestureRecognizer 和可拖动的 MKPinAnnotationView 时遇到问题。
我试图产生的行为类似于地图应用程序。
- 可以拖动图钉。
- 当长按/点击时,会掉落一个图钉。
但是,我无法在 MKPinAnnotationView 的框架外识别长按。如果 Pin 不可拖动,则放置 Pin 的长按手势可以正常工作。但是,当 pin 可拖动时,我无法识别长按手势识别器,因此我可以放下 pin。
有什么想法吗?
顺便说一句,我已经尝试为长按识别器设置委托,以便
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
在这种情况下,可以识别长按手势并放下大头针,但大头针的拖动不再起作用。
MapView 的片段(MKMapView 的子类)
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// init the gesture recognizer
UILongPressGestureRecognizer* lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 0.5f; //user needs to press for 2 seconds
lpgr.delegate = self;
[self addGestureRecognizer:lpgr];
[lpgr release];
//add some initial annotation
Marker *_annotation = [[Marker alloc] initWithCoordinate:_location];
[_annotation titleWithString:@"some title"];
[self addAnnotation:_annotation];
}
return self;
}
- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
{
return;
}
CGPoint touchPoint = [gestureRecognizer locationInView:self];
CLLocationCoordinate2D touchMapCoordinate = [self convertPoint:touchPoint toCoordinateFromView:self];
// add marker to self-map
// Marker is subclass of MKAnnotation
Marker *_annotation = [[Marker alloc] initWithCoordinate:_location];
[_annotation titleWithString:@"some title"];
[self addAnnotation:_annotation];
}
- (MKAnnotationView *)mapView:(MKMapView *)mView viewForAnnotation:(id<MKAnnotation>) annotation {
if([annotation isMemberOfClass:[Marker class]] ) {
// use MKPinAnnotationView for the view
MKPinAnnotationView *_pin = (MKPinAnnotationView *) [mView dequeueReusableAnnotationViewWithIdentifier:@"spot_pin"];
if (_pin == nil)
{
_pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"spot_pin"] autorelease];
}
else
{
_pin.annotation = annotation;
}
[_pin setDraggable:YES];
[_pin setSelected:YES animated:YES];
[_pin setCanShowCallout:YES];
return _pin;
} else {
return nil;
}
}
【问题讨论】:
标签: ios mapkit uigesturerecognizer mkannotationview