我正在使用 handleLongPressGesture 添加注释并在我的 MapView 旁边创建一个文本字段。我的问题是,当我插入子视图时,该函数只被调用一次:
[self createTextfieldForPin:myString :x :y];
有谁知道,为什么?如果没有该功能,每次都会调用handleLongPressGesture-function,当我做一个长按时......
这是我的完整代码:
-(void)handleLongPressGesture:(UIGestureRecognizer*)sender
{
// This is important if you only want to receive one tap and hold event
if (sender.state == UIGestureRecognizerStateEnded)
{
//[self.mapView removeGestureRecognizer:sender];
// [self.mapView removeAnnotations:[self.mapView annotations]];
}
else
{
// Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
CGPoint point = [sender locationInView:self.mapView];
CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
// Then all you have to do is create the annotation and add it to the map
UserAnnotation *dropPin = [[UserAnnotation alloc] initWithCoordinate:locCoord];
[self.mapView addAnnotation:dropPin];
//create NSString from coordinate
NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
[fmt setMaximumFractionDigits:2];
NSString *myString = [[NSString alloc]initWithFormat:@"%@ / %@",[fmt stringFromNumber:[NSNumber numberWithFloat:locCoord.latitude]],[fmt stringFromNumber:[NSNumber numberWithFloat:locCoord.longitude]]];
[self createTextfieldForPin:myString :x :y];
}
}
**编辑
-(void)createTextfieldForPin:(NSString *)coordinates: (CGFloat) _x: (CGFloat) _y
{
self.label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 100.0, 21.0)];
self.label.text = coord;
y = y + 25.0;
self.textfield = [[UITextField alloc]initWithFrame:CGRectMake(x,y,163,31)];
self.textfield.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.label];
[self.view addSubview:self.textfield];
}
**编辑
每次添加图钉时,我还想创建一个标签(用于坐标)和一个文本字段以添加城市或其他内容。就像图片上的一样。但是当 createTextfieldForPin 在该函数中时,我只能调用一次 handleLongPressGesture ..我不知道为什么